Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best way to convert a double to String without decimal places

Tags:

java

math

What is the best way to convert a double to String without decimal places?

What about String.valueOf((int) documentNumber)?

The doubles always have 0 after the decimal dot. I don't need to round or truncate

like image 618
John Alexander Betts Avatar asked Dec 03 '22 17:12

John Alexander Betts


2 Answers

If you are sure that the double is indeed an integer use this one:

NumberFormat nf = DecimalFormat.getInstance();
nf.setMaximumFractionDigits(0);
String str = nf.format(documentNumber);

As a bonus, this way you keep your locale's configuration as in thousand separator.

EDIT
I add this previously removed option as it seems that was useful to the OP:

Double.valueOf(documentNumber).intValue();
like image 73
Paco Abato Avatar answered Jan 18 '23 08:01

Paco Abato


You could try this:

String numWihoutDecimal = String.valueOf(documentNumber).split("\\.")[0];
like image 38
kuki Avatar answered Jan 18 '23 09:01

kuki