如何在java中将带有美元符号的字符串数值转换为BigDecimal?

发布于 2024-12-29 07:46:11 字数 388 浏览 4 评论 0原文

我有一个字符串格式的美元金额。例如:字符串 salePrice = $348.00。

但是,我想将此 String 值转换为 BigDecimal 值,但它在字符串中有美元符号。我尝试了下面的代码,但它不起作用。

BigDecimal sPrice = new BigDecimal(salePrice);

我最终得到了下面这个异常:

java.lang.NumberFormatException
    at java.math.BigDecimal.<init>(Unknown Source)
    at java.math.BigDecimal.<init>(Unknown Source)

I have a dollar amount in a String format. For example: String salePrice = $348.00.

However, I want to convert this String value to a BigDecimal value, but it has a dollar sign in the string. I tried the code below but it isn't working.

BigDecimal sPrice = new BigDecimal(salePrice);

I ended up getting this exception below:

java.lang.NumberFormatException
    at java.math.BigDecimal.<init>(Unknown Source)
    at java.math.BigDecimal.<init>(Unknown Source)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼眸 2025-01-05 07:46:11

BigDecimal 构造函数 采用有效的数字字符串。

字符串表示形式包含一个可选的符号,
'+' ('\u002B') 或 '-' ('\u002D'),后跟零或序列
更多十进制数字(“整数”),可选地后跟
分数,可选地后跟指数。

String salePrice = "$348.00";
String price = salePrice.replace("$","");
BigDecimal sPrice = new BigDecimal(price);
System.out.println(sPrice);

输出 = 348.00

您还可以查看 NumberFormat 类。使用此类,您可以设置相应的Locale

String salePrice = "$123.45";
Locale locale = Locale.US;
Number number = NumberFormat.getCurrencyInstance(locale).parse(salePrice);
System.out.println(number);

输出=123.45

The BigDecimal Constructor take a valid numerical string.

The String representation consists of an optional sign,
'+' ('\u002B') or '-' ('\u002D'), followed by a sequence of zero or
more decimal digits ("the integer"), optionally followed by a
fraction, optionally followed by an exponent.

String salePrice = "$348.00";
String price = salePrice.replace("$","");
BigDecimal sPrice = new BigDecimal(price);
System.out.println(sPrice);

Output = 348.00

You can also look at NumberFormat class. Using this class you can set your corresponding Locale.

String salePrice = "$123.45";
Locale locale = Locale.US;
Number number = NumberFormat.getCurrencyInstance(locale).parse(salePrice);
System.out.println(number);

Output = 123.45

痕至 2025-01-05 07:46:11

首先删除美元符号 ($)。

Remove the dollar sign ($) first.

Bonjour°[大白 2025-01-05 07:46:11

构造函数要求字符串中有一个数字,您的字符串以 $ 开头,这不是有效的数字。你需要先把它去掉。

The constructor requires a Number in the string, your string starts with a $, which is not a valid number. You need to strip it out first.

素年丶 2025-01-05 07:46:11

导入 java.text.DecimalFormat;

var format = new DecimalFormat("$#,###.##", new DecimalFormatSymbols(Locale.US));
format.setParseBigDecimal(true);

return (BigDecimal) format.parse("$348.0");

另请参阅:https:// /www.baeldung.com/java-decimalformat#basic-formatting

注意(!)DecimalFormat 不是线程安全的,因此,在服务中,创建一个 new每次解析之前都会实例化。

import java.text.DecimalFormat;

var format = new DecimalFormat("$#,###.##", new DecimalFormatSymbols(Locale.US));
format.setParseBigDecimal(true);

return (BigDecimal) format.parse("$348.0");

See also: https://www.baeldung.com/java-decimalformat#basic-formatting

NOTE (!) DecimalFormat is not thread-safe, so, in services, create a new instance every time before parsing.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文