This question already has answers here:
Always Round UP a Double
(8 answers)
Closed 5 years ago.
im trying to display a float number in 5 digits rounding up in the last digit, for example : 0.123455 will be displayed as 0.12346, and 0.123454 will be 0.12345.
My current code (not properly working) is as follows:
int iFactorSum = 100000;
int iRoundSum = (int) (fSumatoria * iFactorSum + 0.5);
fSumatoria = (float) iRoundSum / iFactorSum;
fSumatoria storages the number i want to convert.
Thanks
To round up, you can use Math.ceil(numberGrade).
To round to the nearest integer, use Math.round(numberGrade).
See: the Math class
From here.
Use Math.round() to round up floating point numbers
see
https://docs.oracle.com/javase/7/docs/api/java/lang/Math.html#round(double)
Related
This question already has answers here:
How do I get whole and fractional parts from double in JSP/Java?
(18 answers)
Closed 4 years ago.
I have a simple question, I want to know how can I get the decimal part from a double/float number without the dot.
Example: a=0.75 and b=3231.0131
So I would like to set those decimal values in two new Integer variables: m=75 and b=0131.
I'm going to clarify some things, I want to create a new int variable, that variable will storage the decimal part from the original number.
double a = 0.75
double b = 12.033
int x = decimalofa
int y = decimalofb
System.out.println("the decimal of"+a+"is"+x+"and the decimal of"+b+"is"+y)
//the decimal of 0.75 is 75 and the decimal of 12.033 is 033
The thing is that i'm not sure if 033 could be considered as an integer number, so in other words I just want to take all the numbers next to the point and save them in a new variable.
Just do
float a = 0.75f;
System.out.println(Float.toString(a).split("[.]")[1]);
This only works if there is a decimal and there are numbers after that decimal
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 5 years ago.
I have the following variables initialized to the user inputs,
int completedPasses = reader1.nextInt();
int attemptedPasses = reader2.nextInt();
double completionRatio = (250 / 3) * ((completedPasses / attemptedPasses) - 0.3);
How do I round the computation above to a double of one decimal place?
Use DecimalFormat:
DecimalFormat df = new DecimalFormat("#.#");
df.format(completionRatio);
Or Math.round:
Math.round(completionRatio);
This question already has answers here:
Always Round UP a Double
(8 answers)
Closed 7 years ago.
Is there a way to automatically round up a value in java?
For example:
//generate a random integer value
int randomVal = RandomHelper.nextIntFromTo(1, otherVal);
/* then divide the integer value in half... the error I am getting is that its a double, probably
because the number generated isn't an even number, but I NEED it to be an integer. Can I round up?*/
int value = randomVal * 0.5;
You can add 1 then divide by 2 instead of multiplying by 0.5. That way, you avoid a floating point operation followed by a conversion to int.
int value = (randomVal + 1) / 2;
Use Math.ceil():
int value = (int)Math.ceil(randomVal * 0.5);
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
How to Round Decimals to 2 Places after the Decimal Point (Java)
(4 answers)
Closed 7 years ago.
I'm new to java programming. I would like to round up a price to the nearest 2 decimal places with a multiple of 5.
Eg.
38.80 stays the same.
38.81 to 38.80.
38.82 to 38.80.
38.83 to 38.85.
38.84 to 38.85.
38.85 stays the same.
38.86 to 38.85.
38.87 to 38.85.
38.88 to 38.90.
38.89 to 38.90.
38.90 stays the same.
I tried the provided duplicates but they come out only to 1 decimal place.
Eg. 38.82 to 38.8.
This is my code:
import java.text.DecimalFormat;
public class RoundUp {
public static void main(String[] args){
DecimalFormat df = new DecimalFormat("#.##");
double num = 38.84;
System.out.println(df.format(Math.round(num*10.00)/10.00));
}
}
I have looked into other model answers by experts in this web but none of them really answer my question. Setting into 2 decimal places, I'm using DemicalFormat. That I know, but rounding the number, let's say 38.83 to 38.85 and 38.87 to 38.90 is what I really want.
It is a rounding system that my country is using. Can check it out here.
**
This question has been answer by #Mureinik double rounded =
Math.round(num * 100.0 / 5.0) * 5.0 / 100.0;
**
I would recommend you to use BigDecimal instead of double when you are dealing with money.
And then it would be like
BigDecimal value = new BigDecimal(38.84);
value = value.setScale(2, BigDecimal.ROUND_HALF_UP)
You can refer Javadocs for ROUND_HALF_UP and setScale
This question already has answers here:
Round number to only first decimal place
(3 answers)
Closed 8 years ago.
I wish I could understand how it is possible that is not rounded the decimal number obtained from the following code.
File path2 = Environment.getDataDirectory();
StatFs stat2 = new StatFs(path.getPath());
long blockSize2 = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
double result = availableBlocks * blockSize;
free = (Preference)this.findPreference("free_mem");
free.setSummary(Double.toString(result)+" GB");
In a code similar to this use this instruction and works
result = Math.round(result * 10) / 10d;
Why not work here and I still see a number with many decimal places?
If I understood your question right you need NumberFormat here:
NumberFormat nf = NumberFormat.getInstance();
nf.setMaximumFractionDigits(1);
nf.format(result);
This produces a number with 1 decimal places.
So if result is 6.6789 it will produce 6.7.
Related: Round number to only first decimal place
Just a note:
If you do this:
Math.round(result * 10) / 10d;
you basically say:
Multiply result with 10
Round the result
Then divide with ten.
When you got rid of the decimals at step 2. you got another bunch of decimals after the division.