仅显示float的小数

时间:2014-05-23 11:53:03

标签: java floating-point precision

我需要打印float,只显示该浮点数的小数。例如:

1.23456 --> 23456
12.3456 --> 3456
123.456 --> 456

我找到了以下解决方案:

float floatValue = 1.23455f;
String stringValue = Float.toString(floatValue);
int pointIndex = stringValue.indexOf(".");
String decimals = stringValue.substring(pointIndex + 1, stringValue.length() - 1);

但我觉得它有点脏,我想知道是否有其他标准方法,使用String.format或类似的东西。我在文档中找不到任何内容。提前谢谢!

1 个答案:

答案 0 :(得分:1)

float floatValue = 1.23455f;
String stringValue = Float.toString(floatValue).split("\\.")[1];

应该适合你