38

Possible Duplicate:
c# - How do I round a decimal value to 2 decimal places (for output on a page)

I want to truncate the decimals like below

i.e.

  • 2.22939393 -> 2.229
  • 2.22977777 -> 2.229
Community
  • 1
  • 1
ManojN
  • 855
  • 1
  • 11
  • 22
  • Related question: http://stackoverflow.com/questions/304011/truncate-a-decimal-value-in-c – strager Dec 01 '08 at 04:11
  • 1
    I believe the assembly instruction .trn will do this for you as well. – Hamish Grubijan Feb 17 '10 at 01:56
  • 6
    The duplicate question says nothing about rounding off without truncating. While similar, the key aspects of this question (do not round off) is not correctly answered by the "duplicate". Voting to reopen. – psubsee2003 Jun 01 '15 at 13:40
  • 1
    This is not a duplicate question. I couldn't posted my solution here, because the question is closed, so I posted it in a similar thread: http://stackoverflow.com/questions/3143657/truncate-two-decimal-places-without-rounding/37286712#37286712 – nightcoder May 17 '16 at 21:45

10 Answers10

63

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
Christian C. Salvadó
  • 807,428
  • 183
  • 922
  • 838
  • 1
    Doesn't work with the second example. I just want it truncated - not rounded off. – ManojN Dec 01 '08 at 03:50
  • 3
    Just to reiterate: +1 for Math.Truncate, -1 for Math.Round, which doesn't solve the question asked. – Karl Glennon Feb 22 '12 at 10:50
  • Math.Truncate wont work either if you dont multiply and divide it by 1000. Truncate basicly removes the zeros at the end. There is no such a Math.Truncate that takes the number of decimals as a parameter. – 0014 Nov 22 '16 at 20:53
17
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.

Community
  • 1
  • 1
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
7

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
  • 5,973
  • 5
  • 41
  • 47
  • 2
    This fails on large numbers with `System.OverflowException: Value was either too large or too small for an Int32` – sinelaw Aug 02 '12 at 14:27
7

Here's an extension method which does not suffer from integer overflow (like some of the above answers do). It also caches some powers of 10 for efficiency.

static double[] pow10 = { 1e0, 1e1, 1e2, 1e3, 1e4, 1e5, 1e6, 1e7, 1e8, 1e9, 1e10 };
public static double Truncate(this double x, int precision)
{
    if (precision < 0)
        throw new ArgumentException();
    if (precision == 0)
        return Math.Truncate(x);
    double m = precision >= pow10.Length ? Math.Pow(10, precision) : pow10[precision];
    return Math.Truncate(x * m) / m;
}
Glenn Slayden
  • 17,543
  • 3
  • 114
  • 108
3

This is similar to TcKs suggestion above, but using math.truncate rather than int conversions

VB: but you'll get the idea

Private Function TruncateToDecimalPlace(byval ToTruncate as decimal, byval DecimalPlaces as integer) as double 
dim power as decimal = Math.Pow(10, decimalplaces)
return math.truncate(totruncate * power) / power
end function
Jim
  • 31
  • 1
0

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
  • 4,104
  • 2
  • 25
  • 22
0

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
  • 28,090
  • 37
  • 107
  • 148
  • 2
    This doesn't help the OP because it rounds the digits, rather than truncating. Also, the precision specifier for the `"G"` format is for the total number of digits, not just the decimal ones (which is what `"F"` does) – sinelaw Aug 02 '12 at 14:23
0

Try this

double d = 2.22912312515;
int demention = 3;
double truncate = Math.Truncate(d) + Math.Truncate((d - Math.Truncate(d)) * Math.Pow(10.0, demention)) / Math.Pow(10.0, demention);
Pavlo Neiman
  • 7,438
  • 3
  • 28
  • 28
-2

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
  • 25,849
  • 11
  • 66
  • 104
  • 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 Dec 01 '08 at 17:37
-3

Forget Everything just check out this

double num = 2.22939393;
num  = Convert.ToDouble(num.ToString("#0.000"));
sth
  • 222,467
  • 53
  • 283
  • 367