Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
hello friend .....
 
i have textbox in which i enter like 50 or 60 and i want to show result like 50.00 as i leave textbox ....can any buddy help me what i have to do...
 

thanks in advanced
 
Lakhan
Posted 27-Dec-11 5:05am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

string  valueString = string.Format("{0:#.00}", value);
  Permalink  
Comments
Espen Harlinn at 30-Dec-11 9:28am
   
5'ed!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
Espen Harlinn at 30-Dec-11 9:28am
   
5'ed!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hello,
If you enter into textbox a number that belongs to the set of real numbers,
you should know that the value of the text box is still
simple text or string.Entered number as a string shuld be transfered to real number.
So you first need to know whether the user entered number.
For this I usually use TryParse method, afther that if user have
entered the number you should round it if it have more than
two decimal places Math.Round(Number,2),
and the last is to show it in the text box
as a plain text or a string in following format "60" -> "60.00",
by using Number.ToString("### ##0.00") witch transfers real number to simple string with shown string format and round it to two decimals:
 

 
 string Text = " ";
 double Number = 0;
 bool IsReal = false;
			
 Text = textBox1.Text;
 IsReal = double.TryParse(Text,out Number);
 if (IsReal) 
 {
    Number = Math.Round(Number,2);
    textBox1.Text = Number.ToString("### ##0.00");
 }
 else
 {
    MessageBox.Show("You did not entered the number !");
 }
 
Using string format "### ##0.00" inside the Number.ToString(),
you get rounded number to two decimal places so you usually do not need to use
Math.Round(Number,2), and the thousands are separated by space :
 
2.30
123.56
1 236.30
100 001.65
etc.
 
Try reading help instructions for string formatting inside your's programming language.There is a lot more to learn.
 
Bevare the code submited with this solution only works for double type of variables...
 
All the best,
Perić Željko
  Permalink  
v5
Comments
Espen Harlinn at 30-Dec-11 9:29am
   
Nice effort, my 5
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

How about this
 
private void textBox1_Leave(object sender, EventArgs e)
        {
            string str = String.Format("{0:F2}", Double.Parse(textBox1.Text));
            textBox1.Text = str;
        }
  Permalink  
Comments
LAKHANP22 at 28-Dec-11 0:33am
   
THANKS DORABABU743........
Dorababu743 at 28-Dec-11 2:56am
   
Mark as an answer if you get solved...
Espen Harlinn at 30-Dec-11 9:30am
   
Fair enough - 5'ed!
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

Write Code at Leave event of text box.
 
TextBox.Text = (Math.Round(Convert.ToDecimal(TextBox.Text),2)).ToString();
  Permalink  
Comments
Espen Harlinn at 30-Dec-11 9:30am
   
Fair enough - 5'ed!

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

  Print Answers RSS
0 OriginalGriff 350
1 Jochen Arndt 190
2 Richard MacCutchan 135
3 Sergey Alexandrovich Kryukov 130
4 DamithSL 105
0 OriginalGriff 6,045
1 DamithSL 4,601
2 Maciej Los 4,087
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,310


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 28 Dec 2011
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100