toFixed not working for value 3.675 - javascript

I am facing the issue while round numbers in 2 decimal points using javascript function toFixed
It's working for all the values except some cases
For example :
Value is : 3.675 Then it should be 3.68
I have tried it as below
var ans = Number(DiscountValue).toFixed(2);
I have even tried with the following code
var ans = parseFloat(DiscountValue).toFixed(2);
But its returning 3.67
Anyone face this issue before
Plz guide how we can sort this !

Math.round(num * 100) / 100
It will give you the result you are expecting

Related

How to remove the second digit after decimal point using javascript?

i want to remove digits after first integer after decimal point using javascript.
if the input is 2.183 then i want the output to be 2.1 how can i do it. i have seen Math.round and
Math.trunc(). but they dont work as needed.
could someone help me with this. thanks.
Use the Math.floor method. By using value 10 here you will get the answer. Note that if you divide the value by 100, you will get the answer as 2.18
let value = 2.183;
value = Math.floor(value * 10) / 10
console.log(value);

Decimal rounding in JavaScript

I am trying to achieve what was asked in this question. In most scenarios, it is working fine by using the following code:
Math.round(num * 100) / 100
But I encountered a situation where it fails due to a very odd behavior. The number I'm trying to round is 1.275. I am expecting this to be rounded to 1.28, but it is being rounded to 1.27. The reason behind this is the fact that num * 100 is resulting in 127.49999999999999 instead of 127.5.
Why is this happening and is there a way around it?
EDIT
Yes, this question is probably the root cause of the issue I'm facing, but the suggested solutions of the selected answer are not working for me. The desired end result is a rounded number that is displayed correctly. So basically I'm trying to achieve what that question is instructing (check below) but I cannot figure out how.
If you just don’t want to see all those extra decimal places: simply
format your result rounded to a fixed number of decimal places when
displaying it.
You can use toFixed to ensure the precision that you're looking for:
var x = 1.275;
var y = x * 100;
y = y.toFixed(1); // y = 127.5

round numbers in js

I would like to round 45.19202405202648 to 3 decimal places.
I tried :
n.toFixed(3);
also https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/round
Math.round10(n, -3);
myNamespace.round(n, 3);
and
Math.round(n * 1000 + Number.EPSILON ) / 1000;
but these solutions returns 45.19199999999999 insted of 45.192
EDIT : Real question is if I do 65.19202405202648.toFixed(3) - 20 I get 45.19199999999999
Thanks for your help
Your Arithmetical coprocessor is buggy. Try a different computer.
x - number to be rounded; n - number of decimals;
function roundPlus(x, n) {
if (isNaN(x) || isNaN(n)) return false;
var m = Math.pow(10, n);
return Math.round(x * m) / m;
};
Try this:
let num = 45.19202405202648;
console.log(num.toFixed(3));
So it sounds like you've got 2 things going on here. First is the rounding part, which appears to be working as expected. The second issue is just that floating point numbers don't always map well to nice easy to read strings. So, to convert it to a nice string, you can use:
var roundedNum = Math.round(num * 1000) / 1000;
var displayString = roundedNum.toFixed(3)
You need to both round and use .toFixed to get the expected result.
There is a function in javascript called toFixed(). This will return a number with a fixed amount of decimal places. This WILL round 90% of the time but sometimes it does not round perfectly. There is no way to round perfectly without external libraries. For example you would do this:
n.toFixed(3);
I see you have tried this already, it should work. Try and run it again or in a controlled environment.

Javascript coding a calculation

I'm coding a price calculator in JS and I'm stuck with one formula:
number = (parseFloat(newnumber, 10) * parseFloat(1.536, 10)).toString(10);
I want to add 7.44 to the value of newnumber, before it is multiplied with 1.536
I've tried several things, but with no success.
Going to submit this as an answer, even though someone has put this up a comment while I was typing my answer.
number = ((+newnumber + 7.44) * 1.536).toString();
That should give you a string representation of the summed value.
Use parentheses to make the addition before the multiplication.
number = ((parseFloat(newnumber) + 7.44) * 1.536).toString();
Notes: parseFloat doesn't have a radix parameter. There is no reason to parse the number 1.536, that will only turn it to a string and then back to the same number again. The default for the radix parameter for toString is 10, so that isn't needed.
number = ((parseFloat(newnumber) + 7.44) * parseFloat(1.536)).toString();?
Just use parentheses to separate out the operations. Simple fix.
Working DEMO
Try the following code -
var newnumber = '1';
var number = ((parseFloat(newnumber) + 7.44) * parseFloat(1.536)).toString(10);
alert(number);

Javascript round number to 2 decimal places

I have a little problem that I can't seem to get my head round...
I have figure that I need to find what the VAT element of it would be, I have the following jQuery which works but it doesn't fix it to 2 decimal places.
var ThreeMonthPriceFinal = "99.29";
var ThreeMonthPriceVAT = ThreeMonthPriceFinal * 0.2.toFixed(2);
alert(ThreeMonthPriceVAT);
It works out the VAT correctly but adds lots of recurring digits that i don't need... I can't round it up as with VAT you not really supposed to.
http://jsfiddle.net/Xg4Qs/
The Jfiddle shows £19.858000000000004 I need this to show 19.86, i've tried the following but it rounds it up to the whole amount not just 1p.
var ThreeMonthPriceVAT = Math.round(ThreeMonthPriceFinal * 0.2).toFixed(2);
Can anyone help me?
Magic of parentheses
var ThreeMonthPriceVAT = (ThreeMonthPriceFinal * 0.2).toFixed(2);

Categories

Resources