Saving Decimal Values from Text Boxes

I would like to 'save' a decimal value, entered by the user in a text box, to a field in the binary file. So I thought that's easy, just enter an statement like the following:

Prod.W_Condition = CDec(Condition.Text.ToString)

And that should do the trick. But apparently nothing is straight forward in Visual Basic.

When entering a value such as 1234.56 in the text box, I have the value 123456 (without the decimal notation) being saved in W_Condition structure element.

I tried a variety of type convertion such as declaring W_Condition as double and then using Cdbl as convertion function or using Parse.Decimal instead of CDec, but the program just seems to ignore the decimal point in the textbox (e.g Condition.Text) and I end up having an intger value in W_Condition every time.

Is there any way around this The decimal value is entered by the user in the textbox and it must be saved as decimal. So if the user enters 12.56, I have to save it to the field in the binary file (e.g W_Condition) as 12.56 and not as 1256.

Can someone please help me with this problem greatly appreciated!

SJ




Answer this question

Saving Decimal Values from Text Boxes

  • nhd

    In order to store your decimal values in "the dotted format", you could use some help of the US culture, something like in the following sample:

    CultureInfo usCulture = new CultureInfo("en-US");
    decimal
    myNumber = 12.3M;
    string localCultureNumberString = myNumber.ToString();
    // = 12,3
    string usCultureNumberString = myNumber.ToString(usCulture);
    // = 12.3

    usCultureNumberString will always display decimal separator as a dot. To ensure you read back this value property correctly, you can use the same culture when parsing this string:

    decimal myParsedNumber = decimal.Parse(usCultureNumberString, usCulture);

    Andrej



  • CamPeck

    Andrej is most likely correct.

    http://msdn2.microsoft.com/en-us/library/ew0seb73.aspx tells you how to specify how you want to parse the double.

    Best regards,
    Johan Stenberg



  • AnimeKing

    Hi,

    could this be Culture/Regional setting issue Some cultures use commas as decimal separator, some use dots. Have you tried entering "12,56", with the comma as the decimal separator

    Anyway, the following expression will return current culture's number decimal separator:

    System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator

    Andrej



  • stswordman

    Andrej,

    Thank you very much for your prompt reply and useful tip! You were indeed correct.

    I did, actually, think that the System may be using , (comma) as the decimal separator instead of . (period).

    But, last night, when I replaced the period with the comma in my input string, I got an exception handling error but today it run ok and saved 123,456 as 123,456 which leads me to believe that in my haste last night I may have made a mistake with the variable’s type declaration.

    Any way thanks for clarifying the issue.

    I have another question: Is there a way for me to change the current default settings for decimal separator from comma to a period.

    I am using ActiveX to communicate with an external application that uses the ‘period’ as the decimal separator and not the comma.

    My concern is that if I pass the value 123,456 to that application it might interpret the comma as the ‘thousand separator’ and not as the decimal separator.

    The safest would be for my application to store the decimal values using the ‘period’. How would I be able that

    Regards,

    Shariar



  • DMAR330

    Johan,

    Thank you! Please see my reply to Andrej.

    Regards,

    Shariar



  • Saving Decimal Values from Text Boxes