views:

8856

answers:

8

I want to truncate the decimals like below

i.e.

  • 2.22939393 -> 2.229
  • 2.22977777 -> 2.229
+10  A: 

You can use Math.Round:

decimal rounded = Math.Round(2.22939393, 3); //Returns 2.229

Or you can use ToString with the N3 numeric format.

string roundedNumber = number.ToString("N3");

EDIT: Since you don't want rounding, you can easily use Math.Truncate:

Math.Truncate(2.22977777 * 1000) / 1000; //Returns 2.229
CMS
Doesn't work with the second example. I just want it truncated - not rounded off.
mjnagpal
+4  A: 
double d = 2.22977777;
d = ( (double) ( (int) (d * 1000.0) ) ) / 1000.0 ;

Of course, this won't work if you're trying to truncate rounding error, but it should work fine with the values you give in your examples. See the first two answers to this question for details on why it won't work sometimes.

Bill the Lizard
wow all that just to shorten a number.
Nathan W
It looks like a lot, but it's doing a lot less work than converting to a string. :)
Bill the Lizard
it's a more correct way too.
Nathan W
Note you'll probably get rounding errors with this.
strager
You shouldn't get any more rounding error than you start with.
Bill the Lizard
I think using decimal data type should be is for universal solution.
TcKs
Then use decimal instead of double. You're still doing the same operations.
Bill the Lizard
A: 

You can also use Math.Truncate.

decimal decimalNumber;

decimalNumber = 32.7865m;
// Displays 32
Console.WriteLine(Math.Truncate(decimalNumber));

decimalNumber = -32.9012m;
// Displays -32         
Console.WriteLine(Math.Truncate(decimalNumber));

EDIT: Never mind. Didn't notice you're truncating to a given precision.

Cameron MacFarland
A: 

What format are you wanting the output?

If you're happy with a string then consider the following C# code:

double num = 3.12345;
num.ToString("G3");

The result will be "3.12".

This link might be of use if you're using .NET. http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

I hope that helps....but unless you identify than language you are using and the format in which you want the output it is difficult to suggest an appropriate solution.

mezoid
A: 

Try this:

decimal original = GetSomeDecimal(); // 22222.22939393
int number1 = (int)original; // contains only integer value of origina number
decimal temporary = original - number1; // contains only decimal value of original number
int decimalPlaces = GetDecimalPlaces(); // 3
temporary *= (Math.Pow(10, decimalPlaces)); // moves some decimal places to integer
temporary = (int)temporary; // removes all decimal places
temporary /= (Math.Pow(10, decimalPlaces)); // moves integer back to decimal places
decimal result = original + temporary; // add integer and decimal places together

It can be writen shorter, but this is more descriptive.

EDIT: Short way:

decimal original = GetSomeDecimal(); // 22222.22939393
int decimalPlaces = GetDecimalPlaces(); // 3
decimal result = ((int)original) + (((int)(original * Math.Pow(10, decimalPlaces)) / (Math.Pow(10, decimalPlaces));
TcKs
Might be because it doesn't compile. What language were you developing in? I tried this in C# just now and I get an error for trying to multiply a decimal by a double.
Bill the Lizard
A: 

Maybe another quick solution could be:

>>> float("%.1f" % 1.00001)
1.0
>>> float("%.3f" % 1.23001)
1.23
>>> float("%.5f" % 1.23001)
1.23001
magicrebirth
A: 

Forget Everything just check out this

double num = 2.22939393;
num  = Convert.ToDouble(num.ToString("#0.000"));
+2  A: 

A function to truncate an arbitrary number of decimals:

public decimal Truncate(decimal number, int digits)
{
  decimal stepper = (decimal)(Math.Pow(10.0, (double)digits));
  int temp = (int)(stepper * number);
  return (decimal)temp / stepper;
}
Carl Hörberg