why is the result different?
If i use float it get ,675 and if i use double i get ,674… isnt that weird?
float f = 12345.6745;
double d = 12345.6745;
Locale l = Locale.forLanguageTag("es-ES");
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(l);
print(df.format(f));
>> 12.345,675
l = Locale.forLanguageTag("es-ES");
DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(l);
print(df.format(d));
>> 12.345,674
Thanks
Not particularly. You’re formatting different values. In particular, assuming you actually change your code so that it will compile (with an
fsuffix for the float), even though you’re specifying 9 digits,floatwill only reliably represent 7.Neither of the numbers is exactly 12345.6745. In fact, the exact values are:
Look at those and it’s obvious why the third decimal place is 5 for
fand 4 ford.If you want to preserve decimal digits, you should be considering using
BigDecimal.