我需要打印一个 float
,仅显示该 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 或类似的方法。我在文档中没有找到任何内容。提前致谢!
最佳答案
float floatValue = 1.23455f;
String stringValue = Float.toString(floatValue).split("\\.")[1];
应该适合你
关于java - 仅显示 float 的小数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23828530/