Overslaan naar inhoud
Menu
Je moet geregistreerd zijn om te kunnen communiceren met de community.
Deze vraag is gerapporteerd
1 Beantwoorden
2037 Weergaven

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
Annuleer
Auteur Beste antwoord

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
Annuleer