欢迎光临
一个废柴的博客

如何安全的将Java String字符串转换为BigDecimal

实现从Java String字符串到BigDecimal的安装转换:

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Locale;

/**
 * @Auther:IT废柴-https://www.itfeichai.com
 */
public class DecimalFormatExample {
    public static void main(String[] args) {
        String numStr = "1,000,000,000.999999999999999";
        //Locale in_ID = new Locale("en","US");
        //DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance(in_ID);
        DecimalFormat nf = (DecimalFormat) NumberFormat.getInstance();
        nf.setParseBigDecimal(true);

        BigDecimal bd = (BigDecimal) nf.parse(numStr, new ParsePosition(0));

        System.out.println("bd value : " + bd);
    }
}

参考:https://stackoverflow.com/questions/3752578/safe-string-to-bigdecimal-conversion;

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址