简体   繁体   English

将布尔值转换为布尔值(java)

[英]Convert Boolean to boolean (java)

Im trying to convert a String to BigDecimal. 我正在尝试将String转换为BigDecimal。

I found that I need to set DecimalFormat's setparseBigDecimal to true then call parse. 我发现我需要将DecimalFormat的setparseBigDecimal设置为true,然后调用parse。 Unfortunately I keep getting the error that setParseBigDecimal only accepts boolean (not Boolean). 不幸的是,我不断收到setParseBigDecimal仅接受布尔值(而不是布尔值)的错误。 I get that so I tried to do this 我明白了,所以我尝试这样做

DecimalFormat.setParseBigDecimal(Boolean.TRUE.booleanValue())

But I still get the the same error. 但是我仍然遇到相同的错误。 I even tried these but still didnt work 我什至尝试了这些,但还是没用

DecimalFormat.setParseBigDecimal(1)
DecimalFormat.setParseBigDecimal(true)
boolean x= true;
DecimalFormat.setParseBigDecimal(x)

Here's the code. 这是代码。

DecimalFormat.setParseBigDecimal(Boolean.TRUE.booleanValue())
DecimalFormat.parse(AmountUtil.formatForSavingOrConversion(taxAmountList.get(i)),0)
DecimalFormat.setParseBigDecimal(Boolean.TRUE.booleanValue())
DecimalFormat.parse(AmountUtil.formatForSavingOrConversion(taxAmountList.get(i)),0)

won't work because setParseBigDecimal and parse are NOT static methods, you need create an instance of DecimalFormat before you can use it... 由于setParseBigDecimalparse不是static方法而无法使用,因此您需要先创建DecimalFormat实例,然后才能使用它。

DecimalFormat df = new DecimalFormat();

Then... 然后...

df.setParseBigDecimal(Boolean.valueOf(true));
// or
df.setParseBigDecimal(true); 

works fine. 工作正常。 You would then use this instance to parse the values you need 然后,您将使用此实例来解析所需的值

If you have an instance of Boolean , using it as a boolean shouldn't cause any errors (the other way doesn't always work). 如果您有Boolean实例,则将其用作boolean不会引起任何错误(另一种方法并不总是有效)。 However, if you really want an instance of boolean , use one of the following two: 但是,如果您确实想要boolean的实例,请使用以下两种方法之一:

Boolean object = new Boolean(<<<VALUE>>>);
boolean primitive = object;

OR 要么

Boolean object = new Boolean(<<<VALUE>>>);
boolean primitive = object.booleanValue();

Im trying to convert a String to BigDecimal 我正在尝试将字符串转换为BigDecimal

Your question subject and what you are actually trying to do is misleading. 您的问题主题以及您实际尝试做的事情具有误导性。 To parse a string that contains a decimal number you can easily do- 要解析包含十进制数字的字符串,您可以轻松地执行以下操作:

String someValue = "99999999999999999999.99999";
BigDecimal someDecimal = new BigDecimal(someValue);

If your string contains comma or the like, strip them out first. 如果您的字符串包含逗号等,请先将其删除。

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

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