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);
Related
I have a number with an extreme amount of decimal places.
1.5583255870000002e+36
I'm struggling to find a good way to round this down to 2 decimal places using JavaScript.
I've tried a few variations using parseFloat() Math.round() and .toFixed().
Here is an example using .toPrecision().
App.currentUSDBalance = Number((App.currentBalance * App.maticPrice).toPrecision(2));
The result is 1.6e+36. I was hoping for something like 1.59.
Thanks,
The following snippet should be the answer to your updated question:
const yourNumber=1.234567890123e36, ndigits=3;
// returns two variations of the solution:
function mySigDig(num,dig){
return [+(num/(10**Math.floor(Math.log10(num)))).toPrecision(dig),
+num.toPrecision(dig).replace(/e.*/,"")]
}
console.log(mySigDig(23,ndigits));
console.log(mySigDig(2345678,ndigits));
console.log(mySigDig(yourNumber,ndigits));
Apologies for the terrible question.
I ended up solving this by converting to string and dropping the e+X.
App.currentUSDBalance = Number((App.currentBalance * App.maticPrice).toPrecision(3)).toString().split('e')[0];
Thanks for the help.
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
I am attempting to format a currency value to display only 2 decimal digits and also change the output value if negative. Currently, the output is rendering 14 decimal characters and is off by $0.01!
I have a JSFiddle showing the output that depicts these 2 issues I'm having.
The javascript is quite simple! Maybe its simplicity is the cause of the issue. Here's the script:
var accountbalance = 99.99;
var renewalfee = 64.95;
var balanceafterrenewal = (accountbalance - renewalfee);
document.getElementById("remainingBalance").innerHTML = balanceafterrenewal;
The HTML code that renders the output is as follows:
<div class="remainingbalancelabel">Remaining Balance Rendering Output</div>
$<span id="remainingBalance"></span>
</div>
I would appreciate any help in:
Formatting the remainingBalance to correctly display the correct value
Formatting the remainingBalance to display only 2 decimal characters
Color change the remainingBalance to RED when it becomes negative
I'll thank you in advance! :)
Use .toFixed to set to a certain number of decimal places:
document.getElementById("remainingBalance").innerHTML = balanceafterrenewal.toFixed(2);
Assign to an element's .style.color to change its color:
const accountbalance = 99.99;
const renewalfee = 100;
const balanceafterrenewal = (accountbalance - renewalfee);
document.getElementById("remainingBalance").innerHTML = balanceafterrenewal.toFixed(2);
if (balanceafterrenewal < 0) document.getElementById("remainingBalance").style.color = 'red';
<span id="remainingBalance"></span>
I just updated your JSFiddle , simplified things by implementing JQuery and changing the code to verify the value if its below zero, to add your class, and also used the .toFixed(2) to limit the decimal cases.
Hope it will help!
P.S.: Dorry for overriding your fiddle... I'm kinda new to this thing xD
I'm building a plan file for XMPie Uplan. Javascript functions are allowed, so as I am learning JS, I thought I would take advantage of them. They seem friendlier than the proprietary QLingo functions. I don't think that should matter though, it is just JS. Anyone see a problem with my JS?
function cents(p) {
var monfor = toString(parseFloat(Math.round(p * 100) / 100).toFixed(2));
return monfor.slice(-2);
}
The purpose of this code is to return just the cents in a price.
Here is what is supposed to be going on:
First I make sure the number has two decimal places and convert to a string. Then I slice off the last two digits of my string leaving me with a 2 digit integer as a string which is the number of pennies in my price. This will flow into the cents portion of a price field with the cents in superscript. (I have another function that uses floor to kill the sub dollar part of the price that populates the dollar part of the price.) The error on this function is:
Error: cents: An error occurred while executing the function script.
Description: Object expected At line 2 character 0.
Thanks in advance for any help!
var numToParse = parseFloat(Math.round(p * 100) / 100).toFixed(2);
var monfor = numToParse.toString();
return monfor.slice(-2);
You were using the two string in the wrong way.
As mentioned in the comments, i didnt know this, you dont even need to parse it to string as the toFixed() parses it itself.
var monfor = parseFloat(Math.round(p * 100) / 100).toFixed(2);
return monfor.slice(-2);
As you all know since it is one of the most asked topic on SO, I am having problems with rounding errors (it isn't actually errors, I am well aware).
Instead of explaining my point, I'll give an example of what possible numbers I have and which input I want to be able to obtain:
Let's say
var a = 15 * 1e-9;
alert(a)
outputs
1.5000000000000002e-8
I want to be able to obtain 1.5e-8 instead, but I cannot just multiply by 10e8, round and divide by 10e8 because I don't know if it will be e-8 or e-45 or anything else.
So basically I want to be able to obtain the 1.5000002 part, apply toFixed(3) and put back the exponent part.
I could convert into a string and parse but it just doesn't seem right...
Any idea ?
(I apologize in advance if you feel this is one of many duplicates, but I could not find a similar question, only related ones)
Gael
You can use the toPrecision method:
var a = 15 * 1e-9;
a.toPrecision(2); // "1.5e-8"
If you're doing scientific work and need to round with significant figures in mind: Rounding to an arbitrary number of significant digits
var a = 15 * 1e-9;
console.log(Number.parseFloat(a).toExponential(2));
//the above formula will display the result in the console as: "1.50e-8"