Click here to Skip to main content
15,896,154 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi dears.
I have 2 TextBox in my form.
I want to round the number of TextBox1 and show it into TextBox2 with 4 digit after"."
Posted

.ToString("F4") will give you a formatted string with 4 fixed digits:

C#
string fromTextBox = TextBox1.Text;
double num = 0;

if(double.TryParse(fromTextBox, out num){
   // Parsed from string ok..
   string formattedNumber = num.ToString("F4");
}
 
Share this answer
 
v4
Comments
siasalar 13-Aug-12 6:34am    
Thanks alot dude.
i thought it doesnt work.but now i see it works.
StianSandberg 13-Aug-12 6:42am    
great! :)
siasalar 13-Aug-12 6:43am    
you are great sir.
StianSandberg 13-Aug-12 6:45am    
thank you.. Glad I could help.
siasalar 13-Aug-12 6:46am    
Can i add your fb profile?
C#
decimal b = 1.55555555

Math.Round(b, 4); //returns 1.5556
 
Share this answer
 
Comments
StianSandberg 13-Aug-12 6:30am    
Yes, but:
decimal b = 1.5;
Math.Round(b, 4); //returns 1.5
bbirajdar 13-Aug-12 6:44am    
Oh..You are right.. Thank you for this comments and +5 to your solution for letting me know my mistake.. Hope for the same in the future.. :)
StianSandberg 13-Aug-12 6:48am    
thank you, and of course :)
see this example
C#
double a = "2222.2349";//This is Input

string b = Math.Round(a, 3).ToString("0000.0000");
//-> 2222.2350

string c = Math.Round(a, 2).ToString("0000.0000");
//-> 2222.2300

string d = Math.Round(a, 1).ToString("0000.0000");
//-> 2222.2000

string f = Math.Round(a, 0).ToString("0000.0000");
//-> 2222.0000

Happy Coding!
:)
 
Share this answer
 
C#
txt2.Text = Math.Round(Convert.ToDouble(txt1.Text),4, MidpointRounding.AwayFromZero).ToString();
 
Share this answer
 
Comments
StianSandberg 13-Aug-12 6:47am    
if the input value has less than 4 digits, the result of this code will not give the correct format.

1.5 --> 1.5 (not 1.5000)
Santhosh Kumar Jayaraman 13-Aug-12 6:48am    
I think there is no difference in 1.5 and 1.5000.Both will be equal
siasalar 13-Aug-12 7:01am    
in math there is n o difference in 1.5 and 1.5000
but when you use masked text box with mask:00 there is a bit difference in 1.5 and 1.5000
Santhosh Kumar Jayaraman 13-Aug-12 7:04am    
Have u checked?
StianSandberg 13-Aug-12 8:40am    
I agree, but siasalar asked "I want to round the number of TextBox1 and show it into TextBox2 with 4 digit after"." He asked how to get 4 digits in his textbox.
And when converting users input to number you should use a TryParse to avoid exceptions...
I doubt you ever need to round anything. Sometimes it is required, but very rarely. Usually, you only need to give a string representation of the rounded number. Even though it looks like it is done through rounding, this is not exactly so.

The thing is: .NET FCL gives you ready-to-use methods for doing it the way you never round anything by yourself. This is much better, because you don't risk loosing accuracy of calculation by some accidental mistake.

To do such conversion, you need to use the method double.ToString(string format). Read about format string and rounding here:
http://msdn.microsoft.com/en-us/library/kfsatb94.aspx[^],
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

—SA
 
Share this answer
 
Use Math.round method for the above conversion
 
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