开发者

Decimal conversion

I am trying to set decimal values,Below is my input string

String rate="1.000000000";

Converting to double:

Double converted=Double.valueOf(rate);
DecimalFormat format=new DecimalFormat("#.########"); //Setting decimal points to 8

System.out.println("ouput"+format.format(rate)); //Giving output as 1.

I dont understand how to do this,Any hints ple开发者_如何学编程ase.

Regards,

Chaitu


Try

DecimalFormat format=new DecimalFormat("#.00000000");

and

System.out.println("ouput"+format.format(converted));


# will not be displayed for 0, use 0 instead:

    String rate="1.010000000";
    Double converted=Double.valueOf(rate);
    DecimalFormat format=new DecimalFormat("0.00000000");       
    System.out.println("ouput "+format.format(converted));


Firstly, you're passing the string rate to the DecimalFormat.format method. This will fail, you need to pass the converted object in.

When I tested your code with the above changes, I got 1.01 in the output. To format to 8 decimal places, follow Bala Rs comments. i.e. DecimalFormat format = new DecimalFormat("#.00000000");

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜