Java Double to String with dot separator and infinite number of digits after the decimal

mardok

I want to parse a java double value to string but with dot separator and infinite number of digits after the decimal. What I want to achived:

33.123456789 -> "33.123456789"
1.234 -> "1.234"
2.0 -> "2"

I have my sample code but it doesn't work corectly.

DecimalFormat DOUBLE_FORMAT = (DecimalFormat)NumberFormat.getNumberInstance(Locale.ENGLISH);
DOUBLE_FORMAT.applyPattern("#.####");

Which gives me:

33.123456789 -> "33.1234"
1.234 -> "1.234"
2.0 -> "2"

So it not work how I want to. Any idea ? ;)

Joop Eggen

Doubles are just an approximation, a sum of 2-i. Hence

33.123456789 * 100 != 3312.3456789

And

double x = 33.123456789;
String s = String.valueOf(x);
// No guarantee that s has the same number of decimals.

And for the precise numerical type BigDecimal:

BigDecimal x = new BigDecimal(33.123456789); // Bad
BigDecimal x = new BigDecimal("33.123456789"); // Good, with correct precision.

So in your case, one must consider switching to BigDecimal. Or accept String.valueOf / Double.toString.

BigDecimal, though, has that unlimited precision.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Writing number with 2 digits after the decimal separator automatically

Number of decimal digits in a double

Convert String with Dot or Comma as decimal separator to number in JavaScript

Check if 1/n has infinite number of digits after decimal point

java cut string after 2 decimal digits?

How to save digits after decimal point in double number

Get how many decimal digits there is after a dot

Get number of digits after the decimal

Number with decimal separator incorrectly cast to Double

java regex non-capturing groups - extract the whole number portion of a decimal number (digits before the dot)

Convert string with thousands (and decimal) separator into double

Converting Double to String with desired decimal separator

Double to Decimal without rounding after 15 digits

How to remove digits after decimal in a double?

Decimal dot and thousands separator used both in input type number

Always display two digits after decimal separator navision

How to always print 2 digits after the decimal separator?

Angular uses comma instead of dot as decimal separator after changing culture

Decimal separator is changing back to a dot after navigating to another page

bc: set number of digits after decimal point

How to truncate the number of digits after the decimal

Find the number of digits after the decimal point

Java get first 2 decimal digits of a double

Flutter/Dart - Format double to String with thousand separators AND digits after decimal point

How can I round a double number to 1 required decimal digits in Java

Convert string to numeric defining the number of decimal digits

Checking digits with decimal separator defined

finding the number of significant digits versus digits after decimal in r

formatting decimal number to string with comma as decimal separator in racket