Hi, i'm currently using the following code to calculate the total cost of selected services. That works fine. However, when the total price is calculated, some calculations decide to show as £99.9999999 ... Is there a code I can use to prevent this from happening and get it to show £99.99??
This is my current code:
export function iphone4g_click(event) {
$w("#strip2").hide();
$w("#4gstrip").show();
$w("#strip1").show();
$w("#getquote").show();
}
function ttlPrice() {
let charge = 0.00;
if ($w("#switch1").checked) {
charge += Number($w("#switch1").value)
}
if ($w("#switch2").checked) {
charge += Number($w("#switch2").value)
}
if ($w("#switch3").checked) {
charge += Number($w("#switch3").value)
}
if ($w("#switch4").checked) {
charge += Number($w("#switch4").value)
}
if ($w("#switch5").checked) {
charge += Number($w("#switch5").value)
}
if ($w("#switch6").checked) {
charge += Number($w("#switch6").value)
}
if ($w("#switch7").checked) {
charge += Number($w("#switch7").value)
}
if ($w("#switch8").checked) {
charge += Number($w("#switch8").value)
}
return String(charge);
}
export function getquote_click(event, $w) {
$w("#price").text = ttlPrice();
$w("#input1").show();
$w("#input2").show();
$w("#input3").show();
$w("#submit").show();
$w("#yourquote").show();
}
//let's say that the "price" variable value is a number, then: let formattedPrice = price.toLocaleString('en-GB', { style: 'currency', currency: 'GBP' });
The quote is already generated from user input with the code that makes that happen but as you can see in the pic below, it needs a code to prevent the quote from adding those extra digits.
(Design not finished lol)
Using this snippet results in correct behavior for me, not sure where the issue is:
Can you provide more details on how you tried to use it?
Doesn't seem to want to work unfortunately.
Hi!
Try code snippets from this topic: https://stackoverflow.com/questions/6134039/format-number-to-always-show-2-decimal-places
For example, this works just fine when I test it in the Editor:
(Math.round(num * 100) / 100).toFixed(2);
Good luck!