![]() Why using 340 then for setMaximumFractionDigits? Using the ENGLISH locale makes sure you get a point for decimal separator, wherever your program will run. In the French locale, the decimal separator is a comma, not a point: double myValue = 0.00000021d (df.format(myValue)) // Output: 0īy using DecimalFormat, you are local dependent. ![]() (String.format("%.0f", myvalue)) // Output: 0ĭecimalFormat df = new DecimalFormat("0") String.format("%.12f", myvalue) // Output: 0.000000210000īy using setMaximumFractionDigits(0) or %.0f you remove any decimal precision, which is fine for integers/longs but not for double double myValue = 0.00000021d String.format("%s", myvalue) //output: 2.1E-7īy using %f, the default decimal precision is 6, otherwise you can hardcode it, but it results in extra zeros added if you have fewer decimals. (df.format(myValue)) //output: 0.00000021ĭouble.toString() or or FloatingDecimal.toJavaFormatString uses scientific notations if double is less than 10^-3 or greater than or equal to 10^7 double myValue = 0.00000021d If you want to get rid of trailing zeros and locale problems, then you should use: double myValue = 0.00000021d ĭecimalFormat df = new DecimalFormat("0", DecimalFormatSymbols.getInstance(Locale.ENGLISH)) ĭf.setMaximumFractionDigits(340) //340 = DecimalFormat.DOUBLE_FRACTION_DIGITS ![]() Please note that String.format(format, args.) is locale-dependent (see answers below). Please understand the problem before answering. are unacceptable as they both arbitrarily rounds to two decimal places. Sure I can write a function to trim those zeros, but that's lot of performance loss due to string manipulation. I have tried String.format("%f", value), which is close, except I get a lot of trailing zeros for small values. So how do I print these doubles nicely in Java? Given this fact, I choose to use a double type as a single type for all my types, since my largest integer is an unsigned 32-bit number.īut now I have to print these pseudo integers, but the problem is they are also mixed in with actual doubles. A 64-bit double can represent integer +/- 2 53 exactly. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |