how to remove the thousand separator using cultureinfo?
Asked Answered
H

2

6

I try to remove the thousand separator so I'm experimenting some code, but it throws and exception. I tried with Convert.ToDouble, Convert.ToDecimal etc.

it says:

Convert.ToDouble("1.234,45") threw and exception of 'System.FormatException'

The conversion is thrown from line : Convert.ToDouble()

The argument n2 uses the culturinfo, but I also tried "0.00" both throws the same exception

The whole idea is : how to remove the thousand separator, my input is always in this format: 1.234,54 (comma as decimal and dot as thousand separator)... I like to use it in Textbox GotFocus Event.. so the format should be shown like 12345,45

so : 1.254,45 should be 1254,45 and 1.254,00 should be 1254,00

 //http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

CultureInfo ci = CultureInfo.GetCultureInfo("NL-be");
NumberFormatInfo nfi = (NumberFormatInfo)ci.NumberFormat.Clone();
//Now force thousand separator to be empty string
nfi.NumberGroupSeparator = "";
//Format decimal number to 2 decimal places
string decimalFormatted = Convert.ToDouble("1.234,45").ToString("0.00", nfi);
string decimalFormatted = Convert.ToDouble("1.234,45").ToString("n2", nfi);
Hungerford answered 21/3, 2013 at 11:1 Comment(2)
For invariant culture: Convert.ToDouble("1.234,45", CultureInfo.InvariantCulture) else: Convert.ToDouble("1.234,45", CultureInfo.GetCultureInfo("en-gb"))Streaky
both code of lines gives the same error.Hungerford
B
6

Specify the culture with conversion and it will work like:

CultureInfo ci = CultureInfo.GetCultureInfo("NL-be");
double d = Convert.ToDouble("1.234,45", ci);
Bunkhouse answered 21/3, 2013 at 11:3 Comment(3)
thanks this code was working for me. but how does it work? because NL-be has thousand separator a DOT so it should still show the dot.?Hungerford
"@ Habib": I just see an issue: the rule is everything after the comma is a decimal, everything works fine except #1 situation: if I type 1234,00 and leave (it makes: or 123.400,00) and when I type 1234,56 it makes 123.456,00)... so when I use comma as decimal the result is not good... if I type 1234.56 it makes 1.234,56 which is good. So how can I fix 1234,56. it should not make 123.456,00 but it should make 1.234,56. and also type 12,34 becomes 1234,00. please advice?Hungerford
using System.Globalization;Ocko
C
3

Your calls to Convert.ToDouble don't specify a format at all. You need to remember that this:

string decimalFormatted = Convert.ToDouble("1.234,45").ToString("n2", nfi);

is equivalent to:

double tmp = string decimalFormatted = Convert.ToDouble("1.234,45");
string decimalFormatted = tmp.ToString("n2", nfi);

It's the first line that's failing, not the second... and in the first line, it's just going to use the current thread culture.

I suggest you use Double.Parse instead, and specify the format there. (You could use Convert.ToDouble instead, but I generally prefer the more type-specific Double.Parse etc calls. It means there's less to change if you move to TryParse etc.)

You should also consider using decimal instead of double if the exact digits matter. For example, if this is a currency value, it's logically more of a decimal number than a double number.

Crowson answered 21/3, 2013 at 11:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.