简体   繁体   English

将浮点值1000000.1解析为Java中的String

[英]parse float value 1000000.1 into String in java

In my web application I have a version field that take float input values. 在我的Web应用程序中,我有一个采用float输入值的version字段。 But when using values like 1000000.1 (or larger) for the version it displays like 1.0E7. 但是,当使用1000000.1(或更大)之类的值时,其显示为1.0E7。 i tried several methods in Float wrapper class. 我在Float包装器类中尝试了几种方法。 but result still the same. 但结果仍然相同。

Thanks 谢谢

Your problem is not in parsing but in formatting of your values. 您的问题不是解析,而是值的格式。 The value is correct and it is represented in java correctly. 该值正确,并且在Java中正确表示。 If you wish to change the format user either String.format() that provides C style formatting or java.text.NumberFormat. 如果要更改格式用户,请使用提供C样式格式的String.format()或java.text.NumberFormat。

System.out.printf("%f", Float.parseFloat("1.0E7")); outputs 10000000.000000 输出10000000.000000

See http://ideone.com/3o6dO 看到http://ideone.com/3o6dO

Note that 1.0E7 is 1000000.0, not 1000000.1. 请注意1.0E7是1000000.0, 而不是 1000000.1。

它可能与您的输出格式有关,因为1.0E7 == 10000000

我认为您正在寻找的是String.format(locale, msg, args)方法。

Use String.valueOf(floatNumber) 使用String.valueOf(floatNumber)
Suggest use double instead of float 建议使用double而不是float

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM