...

# really confused!!

laylo
09-11-2011, 12:44 AM
var n = 123456.789;
n.toFixed(0);

just starting out JavaScript. So i type the above code in firebug. it returns this result:- "123457"
As for as i know, toFixed() converts a number to a string with a specified number of digits after the decimal point.

If i have specified 0, which i'm assuming it means 0 digits after the decimal point, why does the 7 appear and the 6 is taken out.

really fustrating and can't seem to get my head over it.

DaveyErwin
09-11-2011, 12:56 AM
The toFixed() method formats a number to use a specified number of trailing decimals.

The number is rounded up, and nulls are added after the decimal point (if needed), to create the desired decimal length.

from w3schools

jmrker
09-11-2011, 01:28 AM
To verify the effects of the rounding, try:

var n = 123456.789;
n.toFixed(1);

laylo
09-11-2011, 09:52 AM
just to verify, JS rounds only if that number if 5 or above depending on what value you input to the toFixed() method

jmrker
09-11-2011, 04:26 PM
just to verify, JS rounds only if that number if 5 or above depending on what value you input to the toFixed() method

Yes, whether that is a statement or a question.

xelawho
09-11-2011, 04:51 PM
well, to be finicky it always rounds - but whether it rounds up or down depends on the last decimal it looks at.

if you want to keep your number and just get rid of the decimals you can use parseInt:

var n = 123456.789;
y=parseInt(n);

jmrker
09-11-2011, 06:53 PM
To NOT round, you could:

<script type="text/javascript">
var n = 123456.789;
n = parseInt(n*10)/10