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

Q363 Is there an easy way to change a decimal number string (i.e. .5, .25) into a percentage (i.e. 50%, 25%)?

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

Simple. Multiply by 100 and then round:

<SCRIPT LANGAUGE="JavaScript"><!--
function percent(x) { return Math.round((x-0)*100) + '%'; }

document.write(percent(.5) + '<BR>');
document.write(percent(.25) + '<BR>');
document.write(percent('0.13333') + '<BR>');
//--></SCRIPT>

©2018 Martin Webb