webForumDet fria alternativet
Logga in / Bli medlem

avrundningsfunktion?

JavaScript

5 svar · 305 visningar · startad av Prästen

Medlem sedan dec. 2000537 inlägg
Trådstart#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?

Medlem 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;
Medlem sedan dec. 2000537 inlägg
#3

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

Medlem sedan juli 2003303 inlägg
#4

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

Medlem 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

??

Medlem 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)

259 ms totalt · 4 externa anrop · v20260731065814-full.51f67c91
121 ms — deklarationer (db)
0 ms — hämta statistik (cache)
135 ms — hämta tråd, inlägg och bilagor (db)
119 ms — ändringar (db)