Truncate Decimal to Specified Places

Truncate decimal to specified places

trunc(x*10^4)/10^4

yields 0.1234 like expected.

More generally,

trunc <- function(x, ..., prec = 0) base::trunc(x * 10^prec, ...) / 10^prec;
print(trunc(0.123456789, prec = 4) # 0.1234
print(trunc(14035, prec = -2), # 14000

Truncating a number to specified decimal places

You've answered the question yourself; it seems you just misunderstood what division by zero means. The correct way to do this is to multiply, truncate, then devide, like this:

decimal TruncateTo100ths(decimal d)
{
return Math.Truncate(d* 100) / 100;
}

TruncateTo100ths(0m); // 0
TruncateTo100ths(2.919m); // 2.91
TruncateTo100ths(2.91111m); // 2.91
TruncateTo100ths(2.1345m); // 2.13

There is no division by zero here, there is only division by 100, which is perfectly safe.

Truncate Two decimal places without rounding

value = Math.Truncate(100 * value) / 100;

Beware that fractions like these cannot be accurately represented in floating point.

Truncate but NOT Round in R

You could try with floor()

vars <- floor(vars*100)/100

However, this solution assumes that all values are positive, as floor() rounds to the next lower integer. If there are negative numbers this approach could yield undesirable results:

> floor(-23.441*100)/100
#[1] -23.45

A more general solution therefore consists in using trunc():

vars <- trunc(vars*100)/100

This will remove any digit after the first two decimals, and it also works for negative numbers:

> trunc(-23.441*100)/100
#[1] -23.44

Truncate number to two decimal places without rounding

Convert the number into a string, match the number up to the second decimal place:

function calc(theform) {    var num = theform.original.value, rounded = theform.rounded    var with2Decimals = num.toString().match(/^-?\d+(?:\.\d{0,2})?/)[0]    rounded.value = with2Decimals}
<form onsubmit="return calc(this)">Original number: <input name="original" type="text" onkeyup="calc(form)" onchange="calc(form)" /><br />"Rounded" number: <input name="rounded" type="text" placeholder="readonly" readonly></form>

How can I truncate a double to only two decimal places in Java?

If you want that for display purposes, use java.text.DecimalFormat:

 new DecimalFormat("#.##").format(dblVar);

If you need it for calculations, use java.lang.Math:

 Math.floor(value * 100) / 100;

Truncate (not round) decimal places in SQL Server

select round(123.456, 2, 1)

Truncate (not round off) decimal numbers in javascript

upd:

So, after all it turned out, rounding bugs will always haunt you, no matter how hard you try to compensate them. Hence the problem should be attacked by representing numbers exactly in decimal notation.

Number.prototype.toFixedDown = function(digits) {
var re = new RegExp("(\\d+\\.\\d{" + digits + "})(\\d)"),
m = this.toString().match(re);
return m ? parseFloat(m[1]) : this.valueOf();
};

[ 5.467.toFixedDown(2),
985.943.toFixedDown(2),
17.56.toFixedDown(2),
(0).toFixedDown(1),
1.11.toFixedDown(1) + 22];

// [5.46, 985.94, 17.56, 0, 23.1]

Old error-prone solution based on compilation of others':

Number.prototype.toFixedDown = function(digits) {
var n = this - Math.pow(10, -digits)/2;
n += n / Math.pow(2, 53); // added 1360765523: 17.56.toFixedDown(2) === "17.56"
return n.toFixed(digits);
}

Truncate Decimal number not Round Off

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.

Truncate to three decimals in Python

You can use an additional float() around it if you want to preserve it as a float.

%.3f'%(1324343032.324325235)


Related Topics



Leave a reply



Submit