Click here to Skip to main content
15,885,640 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
textBox7.Text = Convert.ToDouble(textBox1.Text).ToString();

after excuting this line textbox7.text=1.00034333
i.e. textbox.text=1.000;
i want just first 4 characters in textbox7.text. is it possible using truncate?

plz help.
Posted
Updated 6-Oct-15 23:20pm
v2
Comments
phil.o 7-Oct-15 6:29am    
Technically, in "1.000", there are 5 characters, not 4.
In the solution I provided I assumed you want 3 decimal digits. You can change the format specifier if that does not fit your need.

Format the string like below;

C#
String.Format("{0:0.000}", txtbox7.Text); 


I assume that you required 3 decimal points after ".", if your expectation is like that then that would be helpful to you.

You can do this in another way also, you just convert that string into decimal and round the decimal point.
 
Share this answer
 
C#
textBox7.Text = Convert.ToDouble(textBox1.Text).ToString("F3");


You should have a look at MSDN - Standard Numeric Format Strings[^].
 
Share this answer
 
 
Share this answer
 
Comments
ven.in 7-Oct-15 5:25am    
this is an string not double
Suvabrata Roy 7-Oct-15 5:26am    
You cloud convert it to double...
ven.in 7-Oct-15 5:30am    
then again convert into string
Suvabrata Roy 7-Oct-15 5:32am    
then round of it and then convert to string,
Or use split and join to achieve the same
ven.in 7-Oct-15 6:02am    
ya roking. thanks
C#
var str = new String(textbox7.Text.Take(4).ToArray());
//or
var str = textbox7.Text.SubString(0,4);


-KR
 
Share this answer
 
v2
Comments
ven.in 7-Oct-15 5:28am    
it is firing error:
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll

Additional information: Index and length must refer to a location within the string.
Krunal Rohit 7-Oct-15 5:35am    
which one ?

-KR

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