-3

I want values like

1,
1.0,
0 

to be formatted to

1.00,
1.00,
0.00

I'm using the following code,

Double stringToNumber=Double.parseDouble("3");
DecimalFormat toTheFormat = new DecimalFormat("0.00");
toTheFormat.format(stringToNumber)

.format returns a string and if I parse using Double.parseDouble() method.

I lose the precision, i,e 3.00 becomes 3.0 again.How to solve this?

Saravana
  • 12,647
  • 2
  • 39
  • 57
Francis Raj
  • 107
  • 4
  • 12

1 Answers1

0

Would this work? I have made changes

    public class Tester {
    public static void main (String[] args) {
        double d = 1;
        NumberFormat numFormat = NumberFormat.getInstance();
        numFormat.setMaximumFractionDigits(3);
        numFormat.setMinimumFractionDigits(2);
        System.out.println(numFormat.format(d));
    }
}
nomadus
  • 859
  • 1
  • 12
  • 28
  • This will print 1.346 - fine, but if try to print 1, it should print like 1.00, but this code doesn't do so. Still thanks for the reply. – Francis Raj Dec 09 '16 at 04:56
  • It now prints 1.00, if the number is 1. – nomadus Dec 09 '16 at 05:04
  • `numFormat.format(d)` returns a string and i cannot insert number as a string, i can do it, but in excel sheet it would prompt as error. – Francis Raj Dec 09 '16 at 06:25
  • Yes, I agree. This has to be done in JXL side where you set the format of XLS cell to numeric. – nomadus Dec 09 '16 at 06:35
  • Could you try this? WritableCellFormat cf2 = new WritableCellFormat(NumberFormats.FLOAT); Number n = new Number(2,1,3.1415926535,cf2); s.addCell(n); – nomadus Dec 09 '16 at 06:43