0

I have a string:

string test = "19,95";

and I wan't to convert it to an int.

I use:

int num = Convert.Int32(test);

However it throws an FormatException. And tells me that the string is not a proper string for conversion.

My guess is that it has to do with the decimal seperator.

How do i work around this?

Binary Worrier
  • 50,774
  • 20
  • 136
  • 184
Bildsoe
  • 1,310
  • 6
  • 31
  • 44
  • 2
    Should the integer be `20` or should it be `19`? – Binary Worrier Sep 01 '10 at 09:37
  • possible duplicate of [Convert String to Integer](http://stackoverflow.com/questions/1303934/convert-string-to-integer) – H H Sep 01 '10 at 09:38
  • 1
    Can you provide more information about the localisation. Will input always be comma as decimal separator, or can it be a mixture of full stop / comma as the separator. Also do you know the localisation in which the code will be running - does that tie up with the input? – Paul Hadfield Sep 01 '10 at 09:40
  • It am expecting the int to be 20. That for me would be the logical result. Im on a danish windows 7. And comma is always used as decimal seperator. – Bildsoe Sep 01 '10 at 11:38

5 Answers5

4
using System.Globalization;
...
NumberFormatInfo nfi = new NumberFormatInfo();
nfi.NumberDecimalSeparator = ",";
double num = Convert.ToDouble(test, (IFormatProvider)nfi);

Tested & working.

For completeness:

int applesApplesApples = Math.Ceiling(num);
int bananaBananaBanana = (int)num;
int cucumberCucumberCucumber = Math.Floor(num);

[Update]

As Rushyo correctly pointed out in my comments below, this example is contrived and the best practice approach is to identify which culture you are working with in order to get the correct CultureInfo object to work off of.

You can then utilize the localized NumberFormatInfo from that specific CultureInfo when doing all your numeric formatting.

hydrogen
  • 2,878
  • 21
  • 20
  • Why do you want an int.. Someone is going to feel really ripped off if you just rounded their $19,95 up to $20 and didn't give them their 5c change. I have a slight feeling that the OP might have got his types muddled. In any case, I will leave it up to the OP to figure the next bit out if he really does want an int. – hydrogen Sep 01 '10 at 10:07
  • I wan't the int. My working solution right now is to use split to remove the comma and everything after it. Then the convert works. This however also results in the number being rounded down, i want the proper rounding up or down. – Bildsoe Sep 01 '10 at 11:43
  • Cool, do as above (converting to double first) then use: int intVal = (int)doubleVal; to get yourself an int rounded to the nearest integer. – hydrogen Sep 01 '10 at 21:05
  • Use Math.Round. Much, much more consistent (cast the result to an int) – Rushyo Sep 02 '10 at 11:37
  • NOTE: Really not a fan on this solution. .NET has excellent localisation features. Utilising the Danish CultureInfo is going to save you alot of headaches. The classes are there. Use them! – Rushyo Sep 02 '10 at 11:45
  • @Rushyo, Yeah you're right, supporting localization is definately good practice. The contrived example above could be quickly adapted to support this by just extracting the appropriate NumberFormatInfo from your localized CultureInfo. – hydrogen Sep 02 '10 at 20:43
3
string dblText = "19,95";
CultureInfo ci = new CultureInfo ("en-US", true);
ci.NumberFormat.NumberDecimalSeparator = ",";
double dblValue = double.Parse (dblText, NumberStyles.AllowDecimalPoint, ci);

If you need 19 (casting off the decimal part):

int intValue = (int)dblValue;

If you need 20 (mathematical rounding):

int intValue = (int)(dblValue + 0.5);
  • 2
    This would still fail if the localisation was wrong. The string has a comma separator - probably indicating European orgin. If you ran this with a UK or US localisation setting it would fail as it expects a full stop as a separaor. The comma is a thousand separator for UK/US – Paul Hadfield Sep 01 '10 at 09:39
  • Just a note to anyone reading: This answer has since been edited. Question: Why create a new culture that's a variant of en-US? That's just messing up the semantics. 'da' or 'da-DK' seem far more appropriate - and don't require you to create a whole new culture! – Rushyo Sep 02 '10 at 11:43
  • It was just a quick solution to suggest the idea. I like more the solution from hydrogen. –  Sep 02 '10 at 11:47
2

What integer would you expect to get from "19,95"? 1995? 19? 20?

Perhaps you should convert the number to a double first, and then round or truncate in whichever direction makes sense for your application.

sarnold
  • 102,305
  • 22
  • 181
  • 238
1

As the others already mentioned the main problem of your question is, that you don't gave any information about what result you expect and some also brought up the problem with the Culture (decimal separator, thousand separator). So i will make a little sum up:

private int Parse(string text)
{
    Decimal value;

    //Select the culture you like to use
    var culture = CultureInfo.CurrentCulture;
    //var culture = CultureInfo.GetCultureInfo("en-US");
    //var culture = CultureInfo.GetCultureInfo("de-DE");

    if (Decimal.TryParse(text, NumberStyles.Number, culture, out value))
    {
        //Throw away the fractional part
        //return (int)value;

        //or make some rounding??
        return (int)Math.Round(value, MidpointRounding.ToEven);
    }

    //What should happen if the parsing fails??
    //Return some default value
    return int.MinValue;
    //return 0;

    //Or throw an exception?
    //throw new FormatException();
    //In that case, maybe use directly Decimal.Parse and let this
    //function throw the exception with the correct message.
}
Oliver
  • 43,366
  • 8
  • 94
  • 151
0

are you sure you want it to be an int?

this number is a double.

int num = (int)double.Parse(test); //will be 19

double num = double.Parse(test); //wil be 19.95
Stefanvds
  • 5,868
  • 5
  • 48
  • 72