Only 2 Decimal Points in Calculations [SOLVED]

Hey Guys,

Does anyone know a way to get only 2 decimal points when calculations are made on the website ?

I am using the following code to made the calculations

async function getFinalRates(item) {
    $w("#repeater1").forEachItem( ($w, itemData, index) => {
 let adultprice =  (Number($w('#exrate').text)) * (Number(itemData.adultPrice));
    $w('#aprix').text = String(adultprice);
 let teenprice =  (Number($w('#exrate').text)) * (Number(itemData.teenPrice));
    $w('#tprix').text = String(teenprice);
 let childprice =  (Number($w('#exrate').text)) * (Number(itemData.childPrice));
    $w('#cprix').text = String(childprice);
 let infantprice =  (Number($w('#exrate').text)) * (Number(itemData.infantPrice));
    $w('#iprix').text = String(infantprice);
    });
}

I found this on stackoverflow but I don’t understand how to use it in my situation

This would just remove the decimal points and bring it down to 2 though

Math.round(quantity);

Hey shan,

Try this:
let x = parseFloat(quantity).toFixed(2);

I hope his helps,

Yisrael

3 Likes

Works like a charm!!

You’re the best Yisrael. Happy weekend!

1 Like

Hi Yisrael,
If you still available, can you help me to put same that code in my page, below is my code, I need calculated filed “grandtotal” to show only 2 decimals , I tried but can put correctly the code you mentioned above.
Also, if possible show in my code how to mention so that filters of “staffname” and “storename” won’t repeat when new userinput added, so to show only unique feilds without repeating same .

import wixData from ‘wix-data’;

export function button1_click(event, $w) {
// no need to set the filter on the database since it’s still set

// how many items filtered in the dataset?
let count = $w(“#dataset1”).getTotalCount();

// get all of the items
$w(“#dataset1”).getItems(0, count)
.then((results) => {
let sumTotal = 0; // declare sum
let items = results.items;
items.forEach(item => {
// the amount is a string so convert
sumTotal = sumTotal + Number(item.TotalScore);
});
$w(“#grandtotal”).text = “” + sumTotal / + count;
}). catch ((err) => {
console.log(err);

}); 

}

$w.onReady(() => {
$w(‘#dataset1’).onReady(() => {
count();

    $w('#weekno , #storename , #staffname').onChange(() => { 
        search(); 
    }) 
    $w('#clear').onClick(() => { 
        $w('#weekno , #storename , #staffname').value = ""; 
        $w('#dataset1').setFilter(wixData.filter()) 
            .then(count); 
    }); 

function search() {
let filter = wixData.filter();
let weekNo = $w(“#weekno”).value;
let storeName = $w(“#storename”).value;
let staffName = $w(“#staffname”).value;

if (weekNo && weekNo !== ‘all’) {
filter = filter.eq(“weekNo”, weekNo);
}
if (storeName && storeName !== ‘all’) {
filter = filter.eq(“storeName”, storeName);
}
if (staffName && staffName !== ‘all’) {
filter = filter.eq(“staffName”, staffName);
}

        $w('#dataset1').setFilter(filter) 
            .then(count); 
    } 

function count() {
let total = $w(‘#dataset1’).getTotalCount();
if (total > 0) {
$w(‘#textCount’).text = ${total} result has found.;
} else {
$w(‘#textCount’).text = “No result found!”;
}

    }

You will need something like this to show the grandtotal with 2 decimal places:
$w(’ #grandtotal ').text = parseFloat(sumTotal / count).toFixed(2);

Hi Yisrael,
Tnx for reply, though that one I figured out.
But still struggling with my filters of dropdown, In my code I have dropdown filters which is connected to table, but same values repeating as per user inputs. So how I can make those to show only unique values in dropdowns ?
I saw somw posts regarding that but thoese were about repeaters , not with tables and honestly I couldn’t modify my code to work like that. So can you pls advise in my code how to make dropdown filters to show only unique values?

Thanks
Alex