0

So i have this input: 3,000,000

And this function return 30000M:

public static decimal? CustomParse(string incomingValue)
{
    decimal val;
    if (!decimal.TryParse(incomingValue.Replace(",", "").Replace(".", ""), NumberStyles.Number, CultureInfo.InvariantCulture, out val))
        return null;
    return val / 100;
}

What the best way to convert this kind on input to numbber ?

EDIT

I also try this::

Regex regex = new Regex(@"\d+");

And the output is 3

Erofh Tor
  • 159
  • 1
  • 8
  • Possible duplicate of [.NET String.Format() to add commas in thousands place for a number](https://stackoverflow.com/questions/105770/net-string-format-to-add-commas-in-thousands-place-for-a-number) – NibblyPig Mar 05 '19 at 11:06
  • I need from string not toString – Erofh Tor Mar 05 '19 at 11:07
  • Your code already does that, can you clarify your question to show what you are trying to get? – NibblyPig Mar 05 '19 at 11:08
  • You shouldn't replace the "." and as you're dividing by 100 your result seems correct – thumbmunkeys Mar 05 '19 at 11:10
  • Check what your ```Thread.CurrentThread.CurrentCulture``` is set to. Many Cultures use ',' as the decimal point seperator. So depending on your Culture. 3 might very well be the expected outcome. – Max Mar 05 '19 at 11:12
  • I am trying to get 3000000 from 3,000,000 – Erofh Tor Mar 05 '19 at 11:13
  • @ErofhTor, so what about decimal point and what will be the output in case `1,234,567.89` notice here dot(`.`) in between 7 and 8 – er-sho Mar 05 '19 at 11:39

4 Answers4

2

You could also do this

return Decimal.Parse(incomingValue, new CultureInfo("en-US"));

In en-US '.' is the comma seperator. So it will parse the text correctly.

If culture is set to say "de-DE" 3 would be the expected outcome of the conversion as ',' is the comma seperator.

Your regex however will always give you 3, because it just matches [0-9]+ regular expressions don' t care about your culture settings. If you want a regex to match your number you'd have to use something like

Regex regex = new Regex(@"\d+(,\d+)*");

This would match "123" as well as "123,123" but not "123," or "123." (in witch case it will just match the 123 part).

Max
  • 1,536
  • 1
  • 14
  • 18
0

Use IFormatProvider in decimal.Parse

var format = new NumberFormatInfo
{
    NumberGroupSeparator = ","
};

decimal res = decimal.Parse("3,000,000", format);
Alexander
  • 9,104
  • 1
  • 17
  • 41
0

Try this

var culture = new CultureInfo("en-US");
            culture.NumberFormat.NumberDecimalSeparator = ",";
            culture.NumberFormat.NumberGroupSeparator = ".";
            decimal amount = 0;
            if (!decimal.TryParse("3,000,000.12", NumberStyles.Any, culture, out amount))
            {

.... }

-1

You are dividing by 100, so your number won't be 3 millions... Here the solution:

class Program
  {
    static void Main(string[] args)
    {
      decimal? value = CustomParse("3,000,000");
      Console.WriteLine(value);
    }

    public static decimal? CustomParse(string incomingValue)
    {
      decimal val;
      if (!decimal.TryParse(incomingValue.Replace(",", ""), NumberStyles.Number, CultureInfo.InvariantCulture, out val))
        return null;
      return val;
    }
  }
Dave
  • 1,912
  • 4
  • 16
  • 34