6

I have a number var x = 2.305185185185195;

x = x.toFixed(5);

x = 2.30519 but I require this without rounding i.e. 2.30518

I read some thread with two decimal places but could not find for five decimal places.

Any help would be appreciated.

Akki619
  • 2,386
  • 6
  • 26
  • 56
  • Possible duplicate of [Display two decimal places, no rounding](http://stackoverflow.com/questions/4187146/display-two-decimal-places-no-rounding) – Pietro Apr 26 '16 at 10:29

4 Answers4

12

You can use an apropriate factor and floor it and return the result of the division.

Basically this solution moves the point to the left with a factor of 10^d and gets an integer of that and divided the value with the former factor to get the right digits.

function getFlooredFixed(v, d) {
    return (Math.floor(v * Math.pow(10, d)) / Math.pow(10, d)).toFixed(d);
}

var x = 2.305185185185195;

document.write(getFlooredFixed(x, 5));
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
4

If you need only a "part" of a number with a floating point without rounding, you can just "cut" it:

function cutNumber(number, digitsAfterDot) {
    const str = `${number}`;

    return str.slice(0, str.indexOf('.') + digitsAfterDot + 1);
}

const x = 2.305185185185195;

console.log(cutNumber(x, 5)); // 2.30518

This method is fast (https://jsfiddle.net/93m8akzo/1/) and its execution time doesn't depend on number or digitsAfterDot values.

You can also "play around" with both functions in a given fiddle for a better understanding of what they do.


You can read more about slice() method here - MDN documentation


NOTE This function is only an example, don't use it in production applications. You should definitely add input values validation and errors handling!

Andrew Evt
  • 3,613
  • 1
  • 19
  • 35
  • 1
    Please describe in your answer, what was the problem, and how will this snippet solve it, to help others understand this answer – FZs Nov 08 '19 at 22:14
1

The Math.trunc() function returns the integer part of a number by removing any fractional digits

So you can multiply the number by 10^n where n is the desired number of precision, truncate the decimal part using Math.trunc(), divide by the same number (10^n) and apply toFixed() to format it (in order to get the form of 2.30 instead of 2.3 for example)

var x = 2.305185185185195;

console.log((Math.trunc(x*100000)/100000).toFixed(5));
Addis
  • 2,480
  • 2
  • 13
  • 21
0

I have sorted it out by adding a small amount if the decimal is 5, then rounding as usual:

function(value, decimals) {

    var decimals = decimals || 2;

    if( isNaN(value) ){ return 0; }
        
    var decimalPart = value.toString().trim().split('.').pop(),
        extra       = decimalPart.substr(decimals, decimalPart.length - decimals);

    if( extra == '5' && 
        decimalPart.length > decimals
    ){
        value = parseFloat(value) + (1 / ( Math.pow(10, decimals + 5) ) );  
    }

    return Number( parseFloat( value ).toFixed( decimals ) );
        
}
cesar
  • 45
  • 3