Home Articles FAQs XREF Games Software Instant Books About Feedback Search Site-Map
irt.org logo

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

irt.org | Knowledge Base | JavaScript | Number | Q535 [ previous next ]

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

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?'


Provide feedback ...
AddThis Social Bookmark Button

Provide feedback ... AddThis Social Bookmark Button


Last Updated: 6th July 2009. Maintained by: Martin Webb and Michel Plungjan
irt.org liability, trademark, document use, privacy statement and software licensing rules apply.
Copyright © 1996-2009 irt.org, All Rights Reserved.