如何在Java中将常量转换为小数

时间:2014-12-09 23:25:57

标签: java

我有一个像5这样的整数的输入,然后我需要将它转换为像0.05这样的小数。我当前的代码使用String.format,但它不起作用。

System.out.print("Enter the rate as an integer: ");
rI = console.nextInt();
//gets the input
rS = String.format("%0.2f", rI);
//create the string .05
r = Double.parseDouble(rS);
//parse the string rS to a double

使用实例字段

private int rI;
private double r;
private String rS;

1 个答案:

答案 0 :(得分:0)

你的代码,如果它只能添加0,它不会改变数字 只需将您的数字除以100.0d,它将是一个双倍,因为100.0d是双倍

或只是

rS = new String("0.0"+rI);

如果你想说数字55为0.055而不是0.55