在java中将字符串转换为BigDecimal
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28783918/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Convert string to BigDecimal in java
提问by Hari Rao
I am reading a currency from XML
into Java.
我正在从XML
Java 中读取货币。
String currency = 135.69;
When I convert this to BigDecimal
I get:
当我将其转换为时,BigDecimal
我得到:
System.out.println(new BigDecimal(135.69));
Output:
输出:
135.68999999999999772626324556767940521240234375.
Why is it that it outputs this many numbers? How can I avoid this? All I want is for it to output 135.69.
为什么它输出这么多数字?我怎样才能避免这种情况?我想要的只是输出 135.69。
回答by Marlon Patrick
The BigDecimal(double) constructor have some problems, is preferrable that you uses BigDecimal(String) or BigDecimal.valueOf(double).
BigDecimal(double) 构造函数有一些问题,最好使用 BigDecimal(String) 或 BigDecimal.valueOf(double)。
System.out.println(new BigDecimal(135.69)); //135.68999999999999772626324556767940521240234375
System.out.println(new BigDecimal("135.69")); // 135.69
System.out.println(BigDecimal.valueOf(135.69)); // 135.69
The BigDecimal(double) documentation explain about this behavior:
BigDecimal(double) 文档解释了这种行为:
- The results of this constructor can be somewhat unpredictable. One might assume that writing new BigDecimal(0.1) in Java creates a BigDecimal which is exactly equal to 0.1 (an unscaled value of 1, with a scale of 1), but it is actually equal to 0.1000000000000000055511151231257827021181583404541015625. This is because 0.1 cannot be represented exactly as a double (or, for that matter, as a binary fraction of any finite length). Thus, the value that is being passed in to the constructor is not exactly equal to 0.1, appearances notwithstanding.
- The String constructor, on the other hand, is perfectly predictable: writing new BigDecimal("0.1") creates a BigDecimal which is exactly equal to 0.1, as one would expect. Therefore, it is generally recommended that the String constructor be used in preference to this one.
- When a double must be used as a source for a BigDecimal, note that this constructor provides an exact conversion; it does not give the same result as converting the double to a String using the Double.toString(double) method and then using the BigDecimal(String) constructor. To get that result, use the static valueOf(double) method.
- 此构造函数的结果可能有些不可预测。人们可能会假设在 Java 中编写 new BigDecimal(0.1) 会创建一个 BigDecimal,它恰好等于 0.1(未缩放的值为 1,缩放为 1),但它实际上等于 0.100000000000000005551115123125782702341014 这是因为 0.1 不能完全表示为双精度数(或者,就此而言,不能表示为任何有限长度的二进制小数)。因此,传递给构造函数的值并不完全等于 0.1,尽管如此。
- 另一方面,String 构造函数是完全可预测的:编写 new BigDecimal("0.1") 创建一个 BigDecimal,正如人们所期望的那样,它完全等于 0.1。因此,一般建议优先使用 String 构造函数。
- 当 double 必须用作 BigDecimal 的源时,请注意此构造函数提供了精确转换;它与使用 Double.toString(double) 方法然后使用 BigDecimal(String) 构造函数将 double 转换为 String 的结果不同。要获得该结果,请使用静态 valueOf(double) 方法。
回答by Sireesh Vattikuti
String currency = "135.69";
System.out.println(new BigDecimal(currency));
//will print 135.69
回答by Juan Diego
May I add something. If you are using currency you should use Scale(2), and you should probably figure out a round method.
我可以添加一些东西吗?如果您使用货币,则应该使用 Scale(2),并且您可能应该找出一种圆形方法。
回答by sandeep
You are storing 135.69 as String in currency. But instead of passing variable currency, you are again passing 135.69(double value) into new BigDecimal(). So you are seeing a lot of numbers in the output. If you pass the currency variable, your output will be 135.69
您将 135.69 以货币形式存储为字符串。但不是传递可变货币,您再次将 135.69(double value) 传递给 new BigDecimal()。所以你在输出中看到了很多数字。如果您传递货币变量,您的输出将为 135.69
回答by baisakhi chauhan
Hi Guys you cant convert directly string to bigdecimal
嗨,伙计们,您不能将字符串直接转换为 bigdecimal
you need to first convert it into long after that u will convert big decimal
您需要先将其转换为 long 之后您将转换为大十进制
String currency = "135.69";
Long rate1=Long.valueOf((currency ));
System.out.println(BigDecimal.valueOf(rate1));
回答by Neeraj Gahlawat
BigDecimal b = BigDecimal.valueOf(d);
BigDecimal b = BigDecimal.valueOf(d);
import java.math.*;
public class Test {
public static void main(String[] args)
{
// Creating a Double Object
Double d = new Double("785.254");
/// Assigning the bigdecimal value of ln to b
BigDecimal b = BigDecimal.valueOf(d);
// Displaying BigDecimal value
System.out.println("The Converted BigDecimal value is: " + b);
}
}