java - Adding zeros in front of double -
i have double
ex: -2.34
i want add 9 zeros in front of make length fixed 10 digits.
result: -0000000002.34
i tried:
string formatted = string.format("%010d", number);
but can pass double string format function?
what efficient method this?
the easiest way create decimalformat
object, supplying pattern yourself. 0
characters indicate digit should printed here, if unnecessary.
decimalformat df = new decimalformat("0000000000.00"); string formatted = df.format(-2.34);
outputting:
-0000000002.34
with string.format
, can supply total length decimal characters used. use 14
total length, account 10 "whole" digits, negative sign, decimal point, , 2 decimal digits.
string.format("%014.2f", test)
the output is:
-0000000002.34
Comments
Post a Comment