This question already has answers here:
How to nicely format floating numbers to string without unnecessary decimal 0's
(29 answers)
Closed 9 years ago.
How can I make a float value to only show the dot and the decimals if they exist. For example show 17 instead of 17.0 but if I have a 17.2 show the dot and the decimals.
Thanks!
You might also require to limit the length of fraction part, because there might a result like 12.00001 after a sequence of floating point operations. Code snippet I use to nicely format a double to a string:
private static final DecimalFormat[] formats= new DecimalFormat[]{
null,
new DecimalFormat("#.#"),
new DecimalFormat("#.##"),
new DecimalFormat("#.###"),
new DecimalFormat("#.####")
};
public static String toConciseString(double d, int fractionLength){
long asLong = (long) d;
if(Math.abs(d - asLong) < 0.00001d){
return Long.toString(asLong);
}
return formats[fractionLength].format(d);
}
Test cases showing the output examples:
assertThat(toConciseString(23.323, 2)).isEqualTo("23.32");
assertThat(toConciseString(23.329, 2)).isEqualTo("23.33");
assertThat(toConciseString(23.329, 3)).isEqualTo("23.329");
assertThat(toConciseString(23.3, 2)).isEqualTo("23.3");
assertThat(toConciseString(23.30001, 2)).isEqualTo("23.3");
assertThat(toConciseString(23.00001, 2)).isEqualTo("23");
Try this:
DecimalFormat decimalFormat = new DecimalFormat("#0.##");
float float1 = 1.00f;
float float2 = 1.02f;
System.out.println(decimalFormat.format(float1));
System.out.println(decimalFormat.format(float2));
It will print out:
1
1.02
Related
This question already has answers here:
Is floating point math broken?
(31 answers)
Closed 1 year ago.
When I divided the large number into a small number then the division is correct but when I write the small number to divide a large number the answer returns wrong. In my scenario, the small number always be first. here is my code this code return 7.4074074074074075E-6 but the correct result is 0.0000074074.
double itf = 0.0;
double a = 4.0;
double b = 540000;
itf = a / b;
Log.i(TAG, "savedata: outputvalue=" + itf);
BigDecimal a = new BigDecimal("4");
BigDecimal b = new BigDecimal("540000");
// 0.0000074074
a.divide(b, MathContext.DECIMAL128);
You should use a decimal type. double is outside the scope of support
This question already has answers here:
Retain precision with double in Java
(24 answers)
Is floating point math broken?
(31 answers)
Closed 4 years ago.
Eclipse gives the wrong result when trying to calculate the sum of two floats.
In my code, there are 2 float variables: float from = 0.025 and float to = 1.
Then result has double variable: double value = 7 * from / to.
Eclipse compiler shows: value = 0.174999997019767760
In excel calculator, this result was value = 0.175
How can I solve this an issue?
This is simply due to your Java program not rounding the result the same way the excel calculator does. This is a result of the way computers handle floating point arithmetic. You have two options: round the result, or use the java BigDecimal class. If you want to round the result, you can use:
float from = 0.025f;
float to = 1;
double value = 7 * from / to;
DecimalFormat ds = new DecimalFormat("#.###");
double rounded = Double.parseDouble(ds.format(value));
System.out.println(rounded);
If you would rather not have to round, you can use the BigDecimal class.
Java BigDecimal
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 4 years ago.
I have this float 98.01645 and I'm getting 98.02 with this function:
String lastCredit = String.format("%.2f", AppSingleton.getInstance().credit);
That I want is get 98.01 (only two decimals or not rounded number). I'm trying but I can't get the way to do it work.
Doing it manually:
String lastCredit = String.format("%.2f", java.lang.Math.floor(100.0*AppSingleton.getInstance().credit)*0.01);
Multiplying by 100.0 to move the decimal point two to the right, then rounding down, then moving the decimal point two to the left by multiplying with 0.01.
You can try this
String lastCredit = String.format("%.2f",Math.floor((98.01645 * 100)) / 100);
System.out.println(lastCredit);
You basically multiply the value by 100 because you need 2 numbers after the decimal and round down that value. After you have the result you divide it by 100 again.
I would not call this elegant, but you can just use String.format to get 3 decimal places and remove the last using substring:
String lastCredit = String.format("%.3f", AppSingleton.getInstance().credit);
lastCredit = lastCredit.substring(0, lastCredit.length() - 1);
Use below code instead of String lastCredit = String.format("%.2f", AppSingleton.getInstance().credit);
DecimalFormat decimalFormat = new DecimalFormat("#.##");
decimalFormat.setRoundingMode(RoundingMode.FLOOR);
String lastCredit = decimalFormat.format(f);
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Round a double to 2 significant figures after decimal point
i've got lat and long point like this,
x1: 11.955165229802363
y1: 79.8232913017273
i need to convert 4 decimal point
x1 = 11.9552
y1 = 79.8233
Try
double roundTwoDecimals(double d)
{
DecimalFormat twoDForm = new DecimalFormat("#.####");
return Double.valueOf(twoDForm.format(d));
}
Math.ceil(x1* 10000) / 10000
Replace 10000 with 10^N, where N is number of digits after dot. In case of 4 digits after dot, precision shouldn't be lost.
Try this
String.format("%.4f", 11.955165229802363)
Assuming you want to round/truncate the decimal, and speed is not a large consideration, you want to use BigDecimal(BigInteger unscaledVal, int scale) with scale set to 4.
DecimalFormat dtime = new DecimalFormat("#.####");
^^^^
x1= Double.valueOf(dtime.format(x1));
float round = Round(num,4);
System.out.println("Rounded data: " + round);
}
public float Round(float Rval, int Rpl) {
float p = (float)Math.pow(10,Rpl);
Rval = Rval * p;
float tmp = Math.round(Rval);
return (float)tmp/p;
}
If you only want to display the value like that, use a DecimalFormat to convert the value to a string, then display that.
If you really want to round it to four digits, you can achieve that by multiplying by 10000, rounding, then dividing again. However, I would advise against it, since not all decimal numbers can be properly represented in floating point format. Changes are you'll just get something like you already had.
If you really want four digits to work with as an internal state, use a BigDecimal instead. It is properly equipped to do what you want.
This question already has answers here:
How to round a number to n decimal places in Java
(39 answers)
Closed 1 year ago.
I want to truncate a float and a double value in java.
Following are my requirements:
1. if i have 12.49688f, it should be printed as 12.49 without rounding off
2. if it is 12.456 in double, it should be printed as 12.45 without rounding off
3. In any case if the value is like 12.0, it should be printed as 12 only.
condition 3 is to be always kept in mind.It should be concurrent with truncating logic.
try this out-
DecimalFormat df = new DecimalFormat("##.##");
df.setRoundingMode(RoundingMode.DOWN);
System.out.println(df.format(12.49688f));
System.out.println(df.format(12.456));
System.out.println(df.format(12.0));
Here, we are using decimal formatter for formating. The roundmode is set to DOWN, so that it will not auto-round the decimal place.
The expected result is:
12.49
12.45
12
double d = <some-value>;
System.out.println(String.format("%.2f", d - 0.005);
I have the same problem using Android, you can use instead:
DecimalFormat df = new DecimalFormat("##.##");
df.setRoundingMode(RoundingMode.DOWN);
but for this API Level 9 is required.
Another fast solution is:
double number = 12.43543542;
int aux = (int)(number*100);//1243
double result = aux/100d;//12.43
take a look with DecimalFormat() :
DecimalFormat df = new DecimalFormat("#.##");
DecimalFormatSymbols dfs = new DecimalFormatSymbols();
dfs.setDecimalSeparator(',');
df.setDecimalFormatSymbols(dfs);
Check java.math.BigDecimal.round(MathContext).
Try using DecimalFormat and set the RoundingMode to match what you need.