How to truncate double number with adding missing zeros at the end

163 views Asked by At

Consider I have some double number, I want to truncate it(not round) and leave 4 digits after floating point(exactly 4, always 4, if there are not enough digits - add zeros). Examples:

2344.4234934 -----> 2344.4234
1.345584 -----> 1.3455
34.3434 -------> 34.3434
0.123 ------> 0.1230
1 ---------> 1.0000

I'm using java language, and I'm asking for some built-in functions. I have a solution already but it's too complicated, may be there is an elegant one.

1

There are 1 answers

0
Ian McLaird On BEST ANSWER

Try this

DecimalFormat fmt = new DecimalFormat("0.0000");
fmt.setRoundingMode(RoundingMode.DOWN);
String s = fmt.format(myDouble);