0

I have the following piece of code:

decimal kwh = decimal.Parse(textBox1.Text, 
                            NumberStyles.AllowDecimalPoint, 
                            NumberFormatInfo.CurrentInfo);

decimal sale_price = decimal.Parse(textBox2.Text, 
                                   NumberStyles.AllowDecimalPoint, 
                                   NumberFormatInfo.CurrentInfo); 

decimal totalamnt = sale_price * kwh;

My problem is that when the Regional Settings are set to Greek, it doesnt accept any digits after the decimal point, When u enter it with " , "

I tried the InvariantInfo in order to enter the decimals with " . " but again it doesnt accepts them.

The funny thing is that, when you go into Control Panel -> Region and Language -> Additional Settings you can set which operator you want for the decimal points and which for the thousands. If you set the decimal symbol to " . " ....... everything is working perfectly.

Whether you use InvariantInfo and enter it as " . " Or if you CurrentInfo and enter it as " , "

It is driving me crazy i tried many methods, not only the Parse method and the only way to get arround it is to set the decimal symbol to " . " but i cant just go around changing people's regional settings cause i might screw up other Greek software that they are using.

PLEASE people help me .. i am losing my mind with the GREEK language on computers. I would TRULY be GRATEFUL and THANKFUL to any one who can assist me to sort this out

(I set my regional setting to Greek)Insert statement i import 0.25 as sale_price and 5.26 as kwh. I am using a breakpoint and i see the values of the variables are 0.25 and 1.2 using InvariantInfo. When they are stored in the DB are stored as 0 and 5. When i set the decimal operator in the regional settings " . " instead of " , " i import 0.25 and 5.65 and they are stored correctly. When i set the Regional Settings to English Us and i insert 0.25 and 5.65 they are stored correctly

George Georgiou
  • 455
  • 2
  • 14
  • 27
  • 2
    Please post examples of the decimals you are trying to parse. – Oded Apr 21 '12 at 08:11
  • 2
    What has this got to do with MySQL? Your title and tags mention it, but I see nothing which has anything to do with it in the question itself... Please show how you've tried to use InvariantInfo, because that should be fine. – Jon Skeet Apr 21 '12 at 08:11
  • The software is importing values into a MySql DB string initialquery = "INSERT INTO SOLAR_INCOME (ID, E_DATE, KWH, SALE_PRICE, AMMOUNT, T_ID) VALUES ('" + id + "', '" + date.ToString("yyyy:MM:dd") + "', '" + kwh + "', '" + sale_price + "', '" + totalamnt + "', '" + tid + "')"; myMySqlConnection.Open(); MySqlCommand command1 = new MySqlCommand(initialquery, myMySqlConnection); command1.ExecuteNonQuery(); – George Georgiou Apr 21 '12 at 09:41
  • The sale_price and the kwh are decimal numbers – George Georgiou Apr 21 '12 at 09:43
  • (I set my regional setting to Greek)Have look at this up to the Insert statement i import – George Georgiou Apr 21 '12 at 09:56

2 Answers2

0

string ammount = (textBox2.Text).Replace (",",".");

George Georgiou
  • 455
  • 2
  • 14
  • 27
0

if you want to avoid the hassle of changing every ToDecimal() or decimal.Parse() etc. function in your code, try executing the following code at the very start of your application, before loading any form.

Application.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture("en-US")
user1451111
  • 1,735
  • 3
  • 18
  • 30