Click here to Skip to main content
15,901,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi fellows,

need your kind help.. I m having a textbox and want to convert its string value on a lable like below.

string.00

for exmple in texbox there is a value 125 than on lable it should be a decimal value like 125.00



Kindly help.
Posted
Comments
[no name] 5-Apr-13 7:22am    
label1.Text = string.Format("{0:n2}", Textbox1.Text)
VICK 5-Apr-13 7:25am    
But i wanted to do that with .ToDecimal() method... can you help me??
[no name] 5-Apr-13 7:37am    
Where in your question does it say anything about ToDecimal? What do you think ToDecimal is going to do for you when the label text is a string?
[no name] 5-Apr-13 7:40am    
label1.Text = Convert.ToDecimal(Textbox1.Text).ToString("N2")
VICK 5-Apr-13 8:03am    
Thanks alot Phantom... it really works but it shows the commas (,) also... what should i do to remove that??

E.g it shows 16500 to 16,500.00 and i wanted it to show like 16500.00

Try:
C#
myLabel.Text = myTextBox.Text + ".00";



But i wanted to do that with .ToDecimal() method... can you help me??


That sound like homework! :laugh:

So, no code...but it isn't difficult at all.
The first thing you need to do is convert the string value in your text box to a decimal value. Tht's easy: Use the decimal.Parse[^] or decimal.TryParse[^] methods.
Then, you need to convert the decimal value back to a string. Again, easy - just use the decimal.ToString[^] method with an appropriate format string:
"0,0.00"
will do it - it specified that thousands should be separated by commas, and there should always be two decimal places.

This shouldn't take you more than five minutes! :laugh:
 
Share this answer
 
v2
Comments
VICK 5-Apr-13 7:25am    
But i wanted to do that with .ToDecimal() method... can you help me??
OriginalGriff 5-Apr-13 7:38am    
Answer updated
VICK 5-Apr-13 8:14am    
Thanks alot.. it really worked like a charm..

Well Griff I m nt a student who would be having homework.. but yup i a m a new one trying to learn asp... So.. :)
Hope you dont mind.!!!
OriginalGriff 5-Apr-13 8:23am    
You're welcome!

Nope, don't mind. As long as people try to do something and get stuck that's fine. Expecting us to do it so they can go to the bar is another thing altogether! :laugh:
(And we get a lot of that... :sigh: )
VICK 5-Apr-13 8:31am    
Hmm. You are ryt.. actually my concept of decimal was not clear..i tried convert.todecimal before.. but it was not showing the fraction even after 1 hour attempt on google.. but after reading your reply it got cleared that i have to convert it back to string..

So thanks alot again.
I did the following as per my need. and it rocks.. Thanks alot to all of YOU.

Convert.ToDecimal(TextBox1.Text).ToString("0.00");
 
Share this answer
 
Comments
OriginalGriff 5-Apr-13 9:31am    
Don't use Convert.ToDecimal - I suggested Parse or TryParse for a reason: What happens if your user enters "HELLO" instead of a number?
Answer: your application crashes.
Instead, use TryParse:
decimal d;
if (!decimal.TryParse(TextBox1.Text, out d))
{
MessageBox.Show(string.Format("Invalid entry: {0}\nPlease enter a number.", TextBox1.Text));
return;
}

And please, do yourself a favour: don't use Visual Studio default names. Use a name which describes what the textbox (or button, or whatever) is going to be used for: tbPrice for example, or tbTotal.

It may seem pointless on a small task, but it is worth getting into the habit early as it helps your code be self documenting.
VICK 8-Apr-13 2:54am    
Thanks alot for clearing the conept further. but to avoid the crash I have already implemented TextBox Filters to allow only numbers and not the alphabets... But now offcource will do it also as u mentioned above.. and Naming convention is also in my mind.. will try to make it permanent habit as well..

Thanks and warm regards again... :)
OriginalGriff 8-Apr-13 3:44am    
You're welcome!
VICK 8-Apr-13 3:02am    
Again with a minor question... Can u differentiate the Parse & Tryparse.

as per my knowledge TryParse returns a boolean which makes error handling easier while Parse returns an exception... Is It so???
OriginalGriff 8-Apr-13 3:44am    
Yes - exactly that.
I tend to use TryParse (in a block at the top of the method to validate all the controls) so that I can give a specific error message: "The unit price should be a number!" or similar and set teh focus to the "bad" control. The only way to do this with Parse is to have separate tray...catch blocks round each call, which is untidy (and a lot less efficient, since exceptions are slow)

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