Siirry sisältöön
Menu
Sinun on rekisteröidyttävä, jotta voit olla vuorovaikutuksessa yhteisön kanssa.
Tämä kysymys on merkitty
1 Vastaa
2034 Näkymät

I have been learning JavaScript in odd and made this function in POS. It works great, but I want to set the decimal precision to the result number so it only has two decimals.

I've searched a lot and tried different things but none worked for me.

I hope someone have an answer for this.

Example: 

1 / 3 = ‭0.3333333333333333‬    I want the result to be only    0.33

2 / 3 = ‭0.6666666666666667‬     I want the result to be only    0.67

This is the code:

test: function()( 

if (this. bigger){return(this. quantity*3)}

if (this. smaller){return(this. quantity/3)}

return this.  quantity

Avatar
Hylkää
Tekijä Paras vastaus

I was able to set the decimal precision by using tofixed(2)
the correct code would be like this

test: function()( 

if (this. bigger){return(this. quantity*3). tofixed(2) }

if (this. smaller){return(this. quantity/3). tofixed(2) }

return this.  quantity


Avatar
Hylkää