Home Articles FAQs XREF Games Software Instant Books BBS About FOLDOC RFCs Feedback Sitemap
irt.Org
#

Q535 Is there a way I could drop the decimal point in a fraction and make it a whole number?

You are here: irt.org | FAQ | JavaScript | Number | Q535 [ previous next ]

Use the Math object's floor method:

<script language="JavaScript"><!--
alert(Math.floor(12.33333));
alert(Math.floor(9.99));
//--></script>

Simon Bate writes:

The solution doesn't tell the whole story, because if the number is negative, Math.floor will return the next LOWER integer, which is probably not a desirable result. The answer should really be implemented as:

<script language="JavaScript"><!--
function getint(v) {
    if (v<0) {
        return(Math.ceil(v));
    } else {
        return(Math.floor(v));
    }
}
//--></script>

Feedback on 'Q535 Is there a way I could drop the decimal point in a fraction and make it a whole number?'

©2018 Martin Webb