Click here to Skip to main content
15,887,464 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi How can take correct part of my number?
for example:133 from 133.2
Posted

I don't think you ever need to truncate or round any number. There are functions to that, but they are needed in rare esoteric cases.

What you probably need is to present a number in the form of string the way it would look rounded. You really need to do that only when you want to show something on screen.

So, you just need to use formatting via string.Format or double.ToString(string format). For format strings, see:
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx[^],
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx[^].

See also:
http://msdn.microsoft.com/en-us/library/system.string.format.aspx[^],
http://msdn.microsoft.com/en-us/library/kfsatb94.aspx[^],
http://msdn.microsoft.com/en-us/library/d8ztz0sa.aspx[^].

—SA
 
Share this answer
 
Try as below code.
C#
double d = 133.2;

Int32 num1 = Convert.ToInt32(Math.Truncate(d));

//Or as below

Int64 num2 = Convert.ToInt64(Math.Truncate(d));
 
Share this answer
 
Try the below:

C#
int val = Convert.toInt32(inputValue)
 
Share this answer
 
Comments
RaisKazi 20-Nov-11 4:26am    
Voted 3, You try this code for "133.8" and see the result.
Nitesh Kejriwal 20-Nov-11 11:36am    
It would return 134
RaisKazi 20-Nov-11 12:39pm    
Exactly, I think OP wants value before decimal place(In this case it should return 133). Changing my Vote to 4, as OP's requirement is not very clear. Please have a look at my Answer too.
Nitesh Kejriwal 20-Nov-11 15:26pm    
Rais, I agree with your solution too. The question is not very clear..I voted 4 too..
Try
Math.Round Method (Double)[^]
Rounds a double-precision floating-point value to the nearest integral value.
C#
double d = 133.2;
d = Math.Round(d); // d will hold 133 
 
Share this answer
 
v2

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