0

I'm wondering if there's simple way to check if a string is in a value format of any kind. Meaning the number one-thousand could be represented in any of the following ways:

  • 1000
  • 1000.00
  • 1,000
  • 1,000.00
  • $1,000.00

I've tried double.TryParse(value, NumberStyles.Any, CultureInfo.InvariantCulture, out n) but that isn't working for items with the currency symbol out in front. So I tried double.TryParse(value NumberStyles.Any | NumberStyles.AllowCurrencySymbol, CultureInfo.InvariantCulture, out n) but that is also not working.

Basically I'd like to check if a value can be considered a number value at all.

Joe Higley
  • 1,762
  • 3
  • 20
  • 33
  • The invariant culture does not use `$` as its currency symbol. You could try two TryParse calls in succession: one for the current culture, and a fallback for the invariant culture. Include `AllowCurrencySymbol` on both. The order of the calls would depend on what culture you most expect to have been used when formatting the value. – Mike Strobel Sep 22 '17 at 22:49
  • Well it depends on your definition, but you can easily strip out all non-numeric characters and the see if there are any characters left. Something like `var isNumber = (new string("$1,234".Where(c => char.IsDigit(c)).ToArray()).Length > 0);` – Equalsk Sep 22 '17 at 22:52
  • @MikeStrobel What does Invariant Culture use as it's currency symbol and how does CurrentCulture work exactly? – Joe Higley Sep 22 '17 at 22:53
  • @Joe CurrentCulture uses the locale of the host system by default, though it can be overridden (and set on a per-thread basis). It’s also the format that is used by default when formatting values. InvariantCulture uses `¤` as its currency symbol. – Mike Strobel Sep 22 '17 at 22:57
  • Ok so I probably don't want to use invariant then. Thanks. Can you add your reply as an answer please @MikeStrobel – Joe Higley Sep 22 '17 at 22:59

2 Answers2

1

The invariant culture does not use $ as its currency symbol; it uses ¤.

Picking the correct culture for parsing is tricky. Obviously, it is best to use the same culture that was used to format the value to begin with. If you know which culture was used, you’re lucky :).

By default, unless a specific culture is specified, CurrentCulture will be used. The current culture can be overridden and set on a per-thread basis, but it defaults to the locale settings of the host operating system. In the absence of other information, CurrentCulture is a reasonable first guess.

I would suggest you || together at least two TryParse calls: one for CurrentCulture (or your best guess as to the culture of origin), and a fallback using InvariantCulture. Include AllowCurrencySymbol on both.

Mike Strobel
  • 25,075
  • 57
  • 69
0

You can use regular expressions

https://regexone.com/references/csharp

you can use below code to accept your cases

  • 1000
  • 1000.00
  • 1,000
  • 1,000.00
  • $1,000.00

    string pattern = @"^\$?(\d{1,3}(\,\d{3})*(\.0{2})?)|(\d+(\.0{2})?)$";
    Match result = Regex.Match("$100,000.00", pattern);
    if (result.Success) 
      // your input is in format
    
Roohi Ali
  • 628
  • 6
  • 7