4

I have textbox that accept numbers. Those numbers will be saved in database. When I enter number like 2,35 and convert to float and send to database I get error because database accept only number with dot, e.g. 2.35

float num = float.Parse(textBox1.Text);

num is still 2,25

How to manage that? I've tried with CultureInfo.InvariantCulture but I never get what I want

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Josef
  • 2,648
  • 5
  • 37
  • 73
  • why are u using comma instead of dot? – XTGX Apr 04 '13 at 12:34
  • 1
    Where are you getting the error? If it is a problem storing the data in the database, we will need to see your SQL code. – John Koerner Apr 04 '13 at 12:34
  • You could use a simple `string.Replace` to replace the commas with dots. – Nolonar Apr 04 '13 at 12:35
  • 4
    @XTG different cultures use different decimal symbols. Much of Europe uses a comma instead of a dot. – John Koerner Apr 04 '13 at 12:39
  • This may be of some use: http://stackoverflow.com/questions/2583362/how-to-convert-string-to-double-with-proper-cultureinfo – BambooleanLogic Apr 04 '13 at 12:42
  • 1
    @Nolonar, this would replace numbers like "1.000.000,00" (one million) to "1.000.000.00". He must to get current Culture from Operating System (using Thread.CurrentThread.CurrentCulture). Read more [here](http://stackoverflow.com/questions/1542409/how-to-get-current-regional-settings-in-c) – cesarse Apr 04 '13 at 12:42
  • @cesarse Just out of curiosity: how does that culture define the floating point? – Nolonar Apr 04 '13 at 12:46
  • @Nolonar The textbox uses, by default, the same culture as the OS. Typically, when a comma is used as a decimal separator, a point is used as a thousand separator. The CultureInfo class has a GetFormat method that returns a object defining how to format numbers and dates. – cesarse Apr 04 '13 at 13:01
  • The error that I get when saving to database is column count doesn't match value count. This is when I save number with comma. If I use replace and convert comma to dot that value is wrong; number 123,45 became 12345. So how to handle this on different machines with different culture settings? What is the best way for that? – Josef Apr 04 '13 at 13:08
  • How are you saving to the database? If you use parameters properly the original format of the number won't make any difference. – D Stanley Feb 10 '15 at 20:12

5 Answers5

2

You can try the following:

float.Parse(textBox1.Text.Trim(), CultureInfo.InvariantCulture.NumberFormat);

I hope this would've solved the issue.

ShaileshDev
  • 1,086
  • 13
  • 16
1

The easiest way is to replace ',' with '.' in like:

float num = float.Parse(textBox1.Text);
string stringValue = num.ToString().Replace(',', '.');

Then send "stringValue" to database.

I hope that helps you.

Igor Frank
  • 11
  • 1
  • this last solution is good, but not if I want to store textbox value into float property... there must be universal solution for all... – Josef Apr 04 '13 at 14:30
1

use this:

CultureInfo.CreateSpecificCulture("en-US");

I have same problem back then and it solved by code above

Harits Fadillah
  • 341
  • 1
  • 2
  • 8
1

num is 2,25 because it's shown to you in your culture. It will be passed correctly to the database, provided you use the usual mechanisms (i.e. prepared statements with parameters). If you insist on manually gluing together SQL, then by all means use InvariantCulture to format the number, but generally, please don't.

Joey
  • 344,408
  • 85
  • 689
  • 683
1

This is a common globalization issue. What you have to define is a single culture in which to store the data itself, since you are storing it as a string value. Then, do ALL your data input and handling using that culture. In our code, we have several blocks that look similar to this in order to handle multi-cultural math and data display:

//save current culture and set to english
CultureInfo current = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US");

//Do Math and Data things

//restore original culture
System.Threading.Thread.CurrentThread.CurrentCulture = current;

This way you can make sure that all the data is handled and stored the same way, regardless of the culture being use to generate or display the data.

Edit

To do the data save, and converting the number to a string, you would do things exactly the same way. While you have the current threads CultureInfo setting as "en-US", the .ToString() methods from all the numbers will use "." instead of "," for the decimal point. The other way to do it is specify a format provider when calling .ToString().

decimalNumber.ToString(new CultureInfo("en-US"));

This specifies that when you convert the number to a string, use the NumberFormat from the provided culture.

Nevyn
  • 2,623
  • 4
  • 18
  • 32
  • This works fine, saves always on the same way. Decimal input; CultureInfo current = System.Threading.Thread.CurrentThread.CurrentCulture; System.Threading.Thread.CurrentThread.CurrentCulture = new CultureInfo("en-US"); input = decimal.Parse(textBox1.Text.Replace(',','.')); System.Threading.Thread.CurrentThread.CurrentCulture = current; For example if user enters 4,5 or 4.5 the out will be 4,5. This is ok. But if I want to store this value into db-table I must convert to string and replace comma with dot. I want to avoid this. Is there any other way to handle this? – Josef Apr 10 '13 at 10:43