简体   繁体   English

可以在DecimalFormat解析中指定小数位数的确切数目吗?

[英]Is it possible to specify exact number of fractional digits in DecimalFormat parsing?

I'm using DecimalFormat to parse strings representing decimal numbers. 我正在使用DecimalFormat解析代表十进制数字的字符串。 What I'd like to have is to a parsing function which checks the exact number of fractional digits in strings. 我想要的是一个解析函数,该函数检查字符串中小数位数的确切数量。 In details, I want to check that the string has exactly two fractional digits (eg, "1.10" is valid, "1.1" is not valid). 详细地说,我想检查字符串是否恰好有两个小数位数(例如, "1.10"有效, "1.1"无效)。

Is it possible to have such behavior with DecimalFormat ? DecimalFormat是否可能具有这种行为? The instance built with following method doesn't work. 使用以下方法构建的实例不起作用。

private static DecimalFormat decimalFormat() {
    final DecimalFormatSymbols decimalFormatSymbols = new DecimalFormatSymbols();
    decimalFormatSymbols.setDecimalSeparator('.');
    final DecimalFormat decimalFormat = new DecimalFormat("#0.00", decimalFormatSymbols);
    decimalFormat.setMinimumFractionDigits(2);
    decimalFormat.setParseBigDecimal(true);
    return decimalFormat;
}

Any suggestions? 有什么建议么?

Here is the documentation on setMinimumIntegerDigits : https://docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html#setMinimumIntegerDigits-int- 这是有关setMinimumIntegerDigits的文档: https : //docs.oracle.com/javase/8/docs/api/java/text/DecimalFormat.html#setMinimumIntegerDigits-int-

newValue - the minimum number of integer digits to be shown; newValue-要显示的最小整数位数; [...]. [...]。

As far as I can understand, this does not act as a requirement, only as a format for the output. 据我了解,这并不是必需的,只是作为输出的格式。

Looking through the available functions for DecimalFormat , I can't see anything that would give you the number of fraction digits. 查看DecimalFormat的可用函数,我看不到任何能为您提供小数位数的东西。

BUT, since you are using setParseBigDecimal(true) , parsing a string would then give you a BigDecimal which gives you access to the precision function : https://docs.oracle.com/javase/8/docs/api/java/math/BigDecimal.html#precision-- 但是,由于您使用的是setParseBigDecimal(true) ,因此解析字符串将为您提供一个BigDecimal ,使您可以访问precision函数: https : //docs.oracle.com/javase/8/docs/api/java/math /BigDecimal.html#precision--

So what I suggest if you absolutely want to check the result of the parse (and not the string itself) for the correct number of digits is : 因此,我建议您绝对要检查解析结果(而不是字符串本身)的位数是否为:

  • set the minimum fraction digits to 0 将最小分数数字设置为0
  • take the string 带弦
  • parse it in BigDecimal BigDecimal解析它
  • use the BigDecima::precision function to check if the number of digits is correct. 使用BigDecima::precision函数检查位数是否正确。

Hope this helps. 希望这可以帮助。

It sounds like you're trying to validate a condition rather than convert a string to a given format. 听起来您正在尝试验证条件,而不是将字符串转换为给定格式。 You can use regular expressions to do this. 您可以使用正则表达式来执行此操作。 Here's an example method that I believe does what you want 这是我认为可以满足您需求的示例方法

public boolean hasTwoFractionalDigits(String text) {
    return Pattern.compile("^\\d+\\.\\d\\d$").matcher(text).find();
}

This will return true for "10.33" but false for "10.3". 对于“ 10.33”将返回true,但对于“ 10.3”将返回false。 If this doesn't answer your question let me know. 如果这不能回答您的问题,请告诉我。

if price has decimal(.) then decimal(.) always have digit as prefix and suffix both. 如果价格具有小数点(。),则小数点(。)始终以数字作为前缀和后缀。 ie ' 0.0 ' integer price is acceptable. 即' 0.0 '整数价格是可以接受的。 max 2 fractional digit. 最多2个小数位数。

5, 5.0, 5.00 Accept 0, 0.0, 0.00 5. .5, 5.000, ., .0, .00, .000, 0. String = Error 5,5.0,5.00接受0,0.0,0.00 5. .5,5.000,。,.0,.00,.000,0.字符串=错误

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

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