Java中将float转换为String以及将String转换为float
如何将浮点数转换为字符串或将字符串转换为浮点数?
就我而言,我需要在两个值字符串(我从表中获得的值)和我计算的浮点值之间进行断言。
String valueFromTable = "25";
Float valueCalculated =25.0;
我尝试从浮点到字符串:
String sSelectivityRate = String.valueOf(valueCalculated);
但断言失败
How could I convert from float to string or string to float?
In my case I need to make the assertion between 2 values string (value that I have got from table) and float value that I have calculated.
String valueFromTable = "25";
Float valueCalculated =25.0;
I tried from float to string:
String sSelectivityRate = String.valueOf(valueCalculated);
but the assertion fails
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
使用 Java 的
Float
类。要进行比较,最好将字符串转换为浮点数并作为两个浮点数进行比较。这是因为对于一个浮点数有多种字符串表示形式,这些表示形式与字符串比较时是不同的(例如“25”!=“25.0”!=“25.00”等)
Using Java’s
Float
class.To compare it's always better to convert the string to float and compare as two floats. This is because for one float number there are multiple string representations, which are different when compared as strings (e.g. "25" != "25.0" != "25.00" etc.)
浮点到字符串 - String.valueOf()
字符串到浮点 - Float.parseFloat()
Float to string - String.valueOf()
String to Float - Float.parseFloat()
您可以尝试以下代码示例:
此处
You can try this sample of code:
found here
我相信以下代码会有所帮助:
I believe the following code will help:
这是一个可能的答案,这也会给出精确的数据,只需按所需的形式更改小数点即可。
输出将是
This is a possible answer, this will also give the precise data, just need to change the decimal point in the required form.
Output will be
如果您要查找,请说小数点后两位。
<代码>
浮点 f = (浮点)12.34;
String s = new DecimalFormat("#.00").format(f);
If you're looking for, say two decimal places..
Float f = (float)12.34;
String s = new DecimalFormat ("#.00").format (f);
嗯,这种方法不是一个好的方法,但很简单,不推荐。也许我应该说这是最不有效的方法,也是最糟糕的编码实践,但是,使用起来很有趣,
空引号,向变量 str 添加一个空字符串,将 'val' 向上转换为字符串类型。
well this method is not a good one, but easy and not suggested. Maybe i should say this is the least effective method and the worse coding practice but, fun to use,
the empty quotes, add a null string to the variable str, upcasting 'val' to the string type.
将浮点数转换为字符串有三种方法。
有两种方法 将字符串转换为浮点数
示例:-
There are three ways to convert float to String.
There are two ways Convert String to float
Example:-
要走完整的手动路线:此方法通过移动数字的小数点并使用下限(长整型)和模数来提取数字,从而将双精度数转换为字符串。此外,它使用基数除法来计算小数点所属的位置。一旦数字到达小数点后的位数,它还可以“删除”数字的较高部分,以避免丢失超大双精度数的精度。请参阅最后的注释代码。在我的测试中,当 Java 浮点表示实际上显示这些不精确的小数点后位时,它的精确度绝不会低于 Java 浮点表示本身。
请注意,虽然使用 StringBuilder 是为了提高速度,但可以轻松重写此方法以使用数组,因此也适用于其他语言。
To go the full manual route: This method converts doubles to strings by shifting the number's decimal point around and using floor (to long) and modulus to extract the digits. Also, it uses counting by base division to figure out the place where the decimal point belongs. It can also "delete" higher parts of the number once it reaches the places after the decimal point, to avoid losing precision with ultra-large doubles. See commented code at the end. In my testing, it is never less precise than the Java float representations themselves, when they actually show these imprecise lower decimal places.
Note that while StringBuilder is used for speed, this method can easily be rewritten to use arrays and therefore also work in other languages.