简体   繁体   中英

Double.Parse fails for “10.00” retrieved value

The screenshot sums up the problem:

在此处输入图片说明

I have no control over the retrieved value. It comes in with some funky format that I can't figure out and the parsing fails even though it looks totally normal. Typing the value in manually works just fine.

How can I "normalize" the retrieved value so Decimal.Parse does not fail?

For reference, here is the string that fails (copied and pasted):

"‎10.00"

First I would check your regional settings to eliminate anything as simple as a difference in expected decimal separator.

If that draws a blank then if the string 10.00 parses successfully then a string that looks like 10.00 but which fails to parse cannot actually be 10.00 .

Inspect and determine the character code of each character of the string and confirm that it really is 10.00 and not some exotic Unicode that has the same appearance but which is actually different (which may also include characters which are not even visible when displayed).

You might have some kind of special character hidden in the string you are retrieving.

Try this:

Double.Parse(Regex.Replace(decimalValue, @"[^0-9.,]+", ""))

You might need to add using statement for System.Text.RegularExpressions

我只替换一个有问题的字符,这是最安全的选择:

s = s.Replace("\u200E", "");

As Jeroen Mostert mentioned in a comment, there is a non-printed character in your decimalValue .

This is a similar question which should help you deal with that.

https://stackoverflow.com/a/15259355/7636764

Edit:

Using the the string output = new string(input.Where(c => char.IsLetter(c) || char.IsDigit(c)).ToArray()); part of the solution, but also include in || char.IsPunctuation(c) || char.IsPunctuation(c) after IsDigit will get your desired result.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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