Decimal type representation

P

Paolo

I have a table column of SQL smallmoney type which I am updating via a form
input field defined as decimal.

If I enter, say, 51.09 via the decimal input field (representing $51.09),
this is
displayed as 5109.0000 when I view the table data.

I'm not sure why 4 decimal places are being shown, and how would I get the
table data to represent the input i.e. 51.09? I don't want to have to divide
by 10,000
every time I want to display data or do calculations.

Thanks
 
A

Alberto Poblacion

Paolo said:
I have a table column of SQL smallmoney type which I am updating via a form
input field defined as decimal.

If I enter, say, 51.09 via the decimal input field (representing $51.09),
this is
displayed as 5109.0000 when I view the table data.

I'm not sure why 4 decimal places are being shown, and how would I get the
table data to represent the input i.e. 51.09? I don't want to have to
divide
by 10,000
every time I want to display data or do calculations.

Most probably you have fallen into a decimal separator problem. You may
have your client environment set to interpret the comma as a decimal
separator and the dot as a thousands separator, but the data you are keying
in follows the opposite criterion. If you enter "51.09" but the dot is set
to be the thousands separator, you get "5109".

Follow your code carefully, and find out the point at which you are doing
the conversion from text into decimal. Then ensure that you are using the
correct Globalization to match the format in which the numbers are being
entered.

For instance, if you are entering your "51.09" into TextBox1 and you are
converting into decimal like this:

decimal d = decimal.Parse(TextBox1.Text);

Then change it into:

CultureInfo ci = new CultureInfo("en-US");
decimal d = decimal.Parse(TextBox1.Text, ci);

This will always use the period as a decimal separator, regardles of the
CurrentCulture setting.
 
P

Paolo

Alberto: I'm capturing transAmount via a MaskedTextbox whose mask is
$99,999.00 and for which the culture is set to English(Australia).

I have also: msktxtbxAmount.ValidatingType = typeof(System.Decimal);

The code capturing transAmount is:

private void msktxtbxAmount_TypeValidationCompleted(object sender,
TypeValidationEventArgs e)
{
if (!e.IsValidInput)
MessageBox.Show("Invalid amount", "Error",
MessageBoxButtons.OK);
else
transAmount = (decimal)e.ReturnValue; <<<<<<<
}

Perhaps I should divide transAmount by 100 (not 10,000!) before I update the
table?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top