0

Im trying to get a number with precision to 2 decimals, for example this is what I want, if I have the numbers:

3.456 it must returns me 3.45
3.467 = 3.46
3.435 = 3.43
3.422 = 3.42

I don't want to round up or down or whatever just to get the numbers I see 2 places after .

Thanks


Okay, here is the answer

var a = 5.469923;
var truncated = Math.floor(a * 100) / 100; // = 5.46

Thanks everyone for helping.

4 Answers4

2

Assuming Positive Numbers:

The code:

function roundDown(num,dec) {
    return Math.floor(num*Math.pow(10,dec))/Math.pow(10,dec);
}

The test:

function test(num, expected) {
    var val = roundDown(num,2);
    var pass = val === expected;
    var result =  pass ? "PASS" : "FAIL";
    var color = pass ? "GREEN" : "RED";
    console.log("%c" + result + " : " + num + " : " + val, "background-color:" + color);
}

test(3.456, 3.45);
test(3.467, 3.46);
test(3.435, 3.43);
test(3.422, 3.42);

Basic idea:

  • Take number
  • Multiply the number to move decimal place to number of significant figures you want
  • Floor the number to remove the trailing numbers
  • Divide number back to get the correct value

If you want to have a trailing zero, you need to use toFixed(2) which will turn the number to a string.

function roundDown(num,dec) {
    return Math.floor(num*Math.pow(10,dec))/Math.pow(10,dec).toFixed(2);
}

and the test cases would need to change to

test(3.456, "3.45");
test(3.467, "3.46");
test(3.435, "3.43");
test(3.422, "3.42");

Another option is a regular expression.

function roundDown(num,dec) {
    var x = num.toString().match(/(\d*(\.\d{2}))?/);
    return x ? parseFloat(x[0]) : "";
    //return x ? parseFloat(x[0]).toFixed(2) : "";
}
epascarello
  • 204,599
  • 20
  • 195
  • 236
1

Use String operation to achieve it.

var n = 4.56789;
var numbers = n.toString().split('.');
result = Number(numbers[0]+"."+numbers[1].substr(0,2));
alert(result);

Fiddle

Subash Selvaraj
  • 3,385
  • 1
  • 14
  • 17
0

You are looking at the number as if it were a string of digits, rather than a single value, so treat it like a string.-

function cutoff(n, cut){    
    var parts= String(n).split('.'), dec= parts[1];
    if(!cut) return parts[0];
    if(dec && dec.length>cut) parts[1]= dec.substring(0, cut);
    return parts.join('.');
}
var n= 36.938;
cutoff(n,2)

/*  returned value: (String)
36.93
*/

If you want a number, +cutoff(n,2) will do.

kennebec
  • 102,654
  • 32
  • 106
  • 127
0
function truncateDec(num, decplaces) {
    return (num*Math.pow(10,decplaces) - num*Math.pow(10,decplaces) % 1)/Math.pow(10,decplaces);
}
alert(truncateDec(105.678, 2)); // Returns 105.67
alert(truncateDec(105.678, 1)); // Returns 105.6

This could be simplified further if you do not require a dynamic number of decimal places

function truncateDec(num) {
    return (num*100 - num*100 % 1)/100;
}
alert(truncateDec(105.678)); // Returns 105.67

How does it work?

The concept is that the main truncation works by getting the remainder from dividing the original decimal by 1. The remainder will be whatever is in the decimals places. The remainder operator is %

105.678 % 1 = 0.678

By subtracting this remainder from the original number, we will be left with only the integer.

105.678 - 0.678 = 105

To include x number of decimal places, we need to first multiply the original number by 10 to the power of that number of decimal places, thereby shifting the decimal backward by x positions. In this example, we will take x = 2.

105.678 * 10^2 
= 105.678 * 100
= 10567.8

Now, we repeat the same procedure by subtracting the remainder again.

10567.8 % 1 = 0.8
10567.8 - 0.8 = 10567

And to return back to the number of places as requested, we divide it back by 10^x

10567 / 10^2
= 10567 / 100
= 105.67

Hope it helps!

shrmn
  • 1,303
  • 13
  • 19