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?
5 svar · 303 visningar · startad av Prästen
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?
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;
Tack tack, Math.Round hjälpte utmärkt :)
Math.ceil om du vill avrunda uppåt, Math.floor om du vill avrunda neråt.
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)