Click here to Skip to main content
15,886,963 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
int NumValue = 123.6;

string StrValue = NumValue.ToString("00000");


The value assigned to "StrValue" is 00124 instead of 00123 (Means round off).

Please guide me that how to skip rounding off ????
Posted

You can either cast[^] the double to an int or use the Math.Truncate[^] function.

Casting:
C#
int NumValue = (int)123.6;

Truncate:
C#
int NumValue = Math.Truncate( 123.6 );
 
Share this answer
 
v3
Don't assign floating number to int fisrt

C#
float NumValue = 123.6f;
 string StrValue = Math.Floor(NumValue).ToString("00000");
 
Share this answer
 
Well... that won't compile, as you will get a "Cannot implicitly convert type" error.
If you use integers and cast it, you won't get rounded:
C#
int NumValue = (int) 123.6;

string StrValue = NumValue.ToString("00000");

Or, if you are using floating point then you can explicitly say what rounding you want:
C#
double NumValue = (int) 123.6;

string StrValue = Math.Round(NumValue, 0).ToString("00000");
 
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