webForumDet fria alternativet

Avrunda till 1 decimal

4 svar · 839 visningar · startad av JE

JEMedlem sedan juni 2000144 inlägg
#1

Hur skriver man för att avrunda ett tal till endast 1 decimal.
T.ex.: 2.42245 till 2.4 bara.

------------------
/JE

JohnsenMedlem sedan juli 2000718 inlägg
#2

Typ så här: Math.round(2.42245*10)/10
Och för två decimaler: Math.round(2.42245*100)/100
osv...

/ J

JEMedlem sedan juni 2000144 inlägg
#3

Aha, perfekt. Tack

------------------
/JE

ptr_lksMedlem sedan juni 2005588 inlägg
#4

Johnsen skrev:

Typ så här: Math.round(2.42245*10)/10
Och för två decimaler: Math.round(2.42245*100)/100
osv...
/ J

Enligt ovanstående resonemang borde

<input name="knp" type="button" id="knp" onClick="Math.round(document.vvp.points.value=(document.vvp.volym.value/100)*(document.vvp.kcal.value/60 + document.vvp.fett.value/9)*10)/10" value="ge points" />

fungera...
Men det gör inte det, rundar inget av..

Peter SMedlem sedan dec. 20025 484 inlägg
#5

Testa

<input name="knp" type="button" id="knp"
onClick="
  document.vvp.points.value=
   Math.round((document.vvp.volym.value/100) * 
   (document.vvp.kcal.value/60 + document.vvp.fett.value/9) * 10) / 10
" value="ge points" />
Genererad på 369 ms · cache AV · v20260730165559-full.f96bc7eb