gpt4 book ai didi

Java - NumberFormat 成功解析无效字符串

转载 作者:行者123 更新时间:2023-11-30 06:09:29 25 4
gpt4 key购买 nike

我正在尝试检查用户是否以有效格式输入了数字。但似乎无效的字符串也被成功解析。一个例子:

final String value1 = "12,85", value2 = "128,598.77";
NumberFormat format = NumberFormat.getInstance(Locale.GERMAN);
format.parse(value1); // ok
format.parse(value2); // ok, but it's not german format

为什么 format.parse(value2) 不抛出异常?

最佳答案

取自java API

public abstract Number parse(String source, ParsePositionparsePosition)

Returns a Long if possible (e.g., within the range [Long.MIN_VALUE,Long.MAX_VALUE] and with no decimals), otherwise a Double. IfIntegerOnly is set, will stop at a decimal point (or equivalent; e.g.,for rational numbers "1 2/3", will stop after the 1). Does not throwan exception; if no object can be parsed, index is unchanged!

这是预期行为,结果将为 128.598

关于Java - NumberFormat 成功解析无效字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37789729/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com