Convert string to decimal retaining the exact input format

Posted by Brett on Stack Overflow See other posts from Stack Overflow or by Brett
Published on 2010-06-10T10:13:37Z Indexed on 2010/06/10 10:23 UTC
Read the original article Hit count: 434

Filed under:
|
|

Hi All,

I'm sure this is a piece of cake but I'm really struggling with something that seems trivial.

I need to check the inputted text of a textbox on the form submit and check to see if it's within a desired range (I've tried a Range Validator but it doesn't work for some reason so I'm trying to do this server-side).

What I want to do is:

Get the value inputted (eg. 0.02), replace the commas and periods, convert that to a decimal (or double or equivalent) and check to see if it's between 0.10 and 35000.00.

Here's what I have so far:

string s = txtTransactionValue.Text.Replace(",", string.Empty).Replace(".", string.Empty);
        decimal transactionValue = Decimal.Parse(s);
        if (transactionValue >= 0.10M && transactionValue <= 35000.00M) // do something

If I pass 0.02 into the above, transactionValue is 2. I want to retain the value as 0.02 (ie. do no format changes to it - 100.00 is 100.00, 999.99 is 999.99)

Any ideas?

Thanks in advance, Brett

© Stack Overflow or respective owner

Related posts about c#

Related posts about beginner