Click here to Skip to main content
15,888,527 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to convert the text box value to decimal(3,2) like 3.22 ,4.33, etc
i tried to divide the text box value by 100 but it is converting the decimal values to 0 like for 123 it is storing 1.00 but i want 1.23 to be stored in db.

please help.
Posted

There is no such thing as decimal(3,2) or something like that. It looks like you mix up different things: data, numeric types, and string representation of numeric data. You need to work with data, not strings. And don't put strings in a database where you can use numeric types. If you do it, you need to fix the database schema.

You problem is merely a problem of formally of decimal data when you present it somewhere on the screen. Please see:
http://msdn.microsoft.com/en-us/library/364x0z75%28v=vs.80%29.aspx[^].

For string formatting of decimal values, please see:
http://msdn.microsoft.com/en-us/library/system.decimal.tostring.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

—SA
 
Share this answer
 
Comments
__TR__ 26-Dec-12 6:17am    
My 5.
Sergey Alexandrovich Kryukov 26-Dec-12 12:24pm    
Thank you.
—SA
If you divide the textbox value by 100 123 will be shown as 1.00 because of integer division. To solve this you should follow the below code:-
C#
double val = 537/100f; // f tells compiler to perform float division
Console.WriteLine(val.ToString("#0.00"));


Reply if this solves your problem. :)
 
Share this answer
 
this way also, this problem can be solved...
C#
string s = (Convert.ToDouble(textbox1.Text) / 100).ToString("0.00");

Happy Coding!
:)
 
Share this answer
 
Decimal (3,2) means total length is 3, out of which 2 are reserved for decimal. Then nothing will be before decimal.

Ex:- Decimal(12,8) means total length 12 out of which 8 before decimal and 4 after decimal.

So increase the length of your field in database
May it helps you
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900