webForumDet fria alternativet

avrundningsfunktion?

5 svar · 303 visningar · startad av Prästen

PrästenMedlem sedan dec. 2000537 inlägg
#1

Hello jag undrar över ett par saker.

1. Finns det någon avrundningsfunktion i javascript som avrundar ett värde

2. Vilken variabeltyp kan innehålla decimaler i Java-Script?

dectgapMedlem sedan sep. 20021 655 inlägg
#2

1. Math.round() avrundar tal:
http://devedge.netscape.com/library/manuals/2000/javascript/1.3/reference/math.html#1197725

2. Flyttal kan innehålla decimaler, men du behöver inte ange att en variabel ska vara av typen flyttal:

var approxPI = 3.1415;
PrästenMedlem sedan dec. 2000537 inlägg
#3

Tack tack, Math.Round hjälpte utmärkt :)

CheguelinMedlem sedan juli 2003303 inlägg
#4

Math.ceil om du vill avrunda uppåt, Math.floor om du vill avrunda neråt.

PrästenMedlem sedan dec. 2000537 inlägg
#5

aha, men round då?, tar några exempel.

avrundar den såhär?

1.51=2
1.49=1

??

dectgapMedlem sedan sep. 20021 655 inlägg
#6

Citat från länken jag gav dig:

Description
If the fractional portion of number is .5 or greater, the argument is rounded to the next higher integer. If the fractional portion of number is less than .5, the argument is rounded to the next lower integer.

Because round is a static method of Math, you always use it as Math.round(), rather than as a method of a Math object you created.

Examples

//Returns the value 20
x=Math.round(20.49)

//Returns the value 21
x=Math.round(20.5)

//Returns the value -20
x=Math.round(-20.5)

//Returns the value -21
x=Math.round(-20.51)

135 ms totalt · 3 externa anrop · v20260731065814-full.30151723
132 ms — hämta forumlista (db)
0 ms — hämta statistik (cache)
124 ms — hämta tråd, inlägg och bilagor (db)