Search code examples
c#globalization

NumberFormatInfo not removing decimals


This should be really simple and straight forward but for some reason it won't work.

var myNumber = "100255.123";

var numberFormatInfo = new NumberFormatInfo
{CurrencyDecimalDigits = 0, NumberDecimalDigits = 0, PercentDecimalDigits = 0};

var noDecimals = decimal.Parse(myNumber, numberFormatInfo);

//noDecimals = 100255.123 ???

For context reasons I do need to parse this string as a decimal and not an integer.

Shouldn't the "noDecimals" variable be parsed as "100255" without any decimal digits after applying the NumberFormatInfo?

How come this is not working?

Thanks for your help.


Solution

  • Sounds like you just want to truncate:

    http://msdn.microsoft.com/en-us/library/system.decimal.truncate.aspx

    var myNumber = "100255.123";
    
    var noDecimals = decimal.Truncate(decimal.Parse(myNumber));