Freshman
Freshman

Reputation: 303

Limit the number of decimals writen (js)

I got a minor brain-teaser thats keep bothering me. I'm unable to control the numbers of decimals written by my scripts.

HTML

<script type="text/JavaScript">
    <!--
    document.write($tekn_2_2/$tekn_2_1)
    //-->
</script>

JS

$tekn_2_1 = "28"
$tekn_2_2 = "7600"

I want this to return the value '271' instead of '271.42857142857144'. I apologize, I'm aware that this should be a small case, but I can get anything to work.

Upvotes: 0

Views: 94

Answers (2)

jholloman
jholloman

Reputation: 1979

Edit: Originally I had voted for toFixed but was not aware that this function also rounds. It looks like a simple floor is all that you need.

Math.floor($tekn_2_2/$tekn_2_1) == 271

Edit: +1 for Javascript: The Good Parts it goes over basic operations such as this. In a short but very informative manner.

http://www.amazon.com/JavaScript-Good-Parts-Douglas-Crockford/dp/0596517742

Upvotes: 1

srijan
srijan

Reputation: 1512

document.write(Math.round($tekn_2_2/$tekn_2_1));

Upvotes: -1

Related Questions