View Full Version : A way to output decimal numbers with ".0"?
Dylan Leblanc
09-17-2002, 10:58 PM
Hi,
On my website I want to output an array of numbers with decimal places. I have the loop and the array all set up to output them, and it works, but numbers such as 8.0 or 10.0, or anything ending in ".0", get output simply as "8" or "10" with no ".0".
Is there a Javascript function that will format the output so that I get ".0" to be output? Something along the line for formatting ability of C's printf() function? So that I can specify to output with one decimal digit?
oops, didn't read your question properly. Disregard previous post. Can you enter the numbers into the array as strings perhaps?
adios
09-18-2002, 03:44 AM
Just happen to be working on a JS-equiv for Perl's printf(); a little trickier than I thought so, you can try this custom Number class method:
<html>
<head>
<title>untitled</title>
</head>
<body>
<pre>
<script type="text/javascript" language="javascript">
///////////////////////////////////////////////////////////////////////////
Number.prototype.toDecPlaces = function(places, unrounded) {
var temp = this.toString(10), tempplaces = places;
if (temp.indexOf('.') == -1) {
temp += '.';
while (places--) temp += '0';
return temp;
} else if (!unrounded) {
temp = Number(temp);
temp = Math.round(temp * Math.pow(10, places)) / Math.pow(10, places);
}
temp = temp.toString(10);
while (tempplaces--) temp += '0';
return temp.substring(0, temp.lastIndexOf('.') + (places + 1));
}
///////////////////////////////////////////////////////////////////////////
function demo() {
var x = Number(prompt('Number?','number'));
var places = Number(prompt('Decimal places?','places'));
var unrounded = !confirm('Rounded?');
alert(x.toDecPlaces(places,unrounded));
}
document.write(Number.prototype.toDecPlaces);
</script>
</pre>
<a href="javascript:void demo()">run it</a>
</body>
</html>
The default is to round off numbers....
nolachrymose
09-19-2002, 02:53 AM
function myFunc(num) {
return num.toFixed(1);
}
Hope that helps!
Happy coding! :)
adios
09-19-2002, 03:08 AM
Don't believe Number.toFixed() is supported in IE4, NS4, Opera 4,5, and others.
realisis
09-19-2002, 12:05 PM
if toFixed() isn't supported, here's an alternate way:
function checkMod()
{
for(n=0; n<MyNumbersArray.length; n++)
{
num = MyNumbersArray[n]
mod = num%1
if (mod==0) num = num+".0";
return num
}
}
adios
09-19-2002, 05:45 PM
Two problems there: your loop goes nowhere (return statement), and, more importantly, the function returns floating-point numbers as numbers, but converts integers to strings by concatenation.
<html>
<head>
<title>untitled</title>
<script type="text/javascript" language="javascript">
var MyNumbersArray = [1,1.8]
function checkMod()
{
for(temparr=[],n=0; n<MyNumbersArray.length; n++)
{
num = MyNumbersArray[n]
mod = num%1
if (mod==0) num = num+".0";
temparr[n]=num
}
return temparr
}
var x = checkMod()
alert(x[0] + ' is ' + typeof x[0])
alert(x[1] + ' is ' + typeof x[1])
</script>
</head>
<body>
</body>
</html>
This bothers me...but maybe it's not significant. Anyway, easily 'fixed', nice idea. :thumbsup:
realisis
09-21-2002, 06:27 PM
adios:
"the function returns floating-point numbers as numbers, but converts integers to strings by concatenation."
Yes agreed - it does. Since, according to the original question, the values were to be dumped directly to screen, and presumably no further calculations would be forthcoming. I didn't think it was worth converting them to the same typeof.
The quickest fix would be just to state:
if (mod==0) num = num+".0";
else num = num.toString();
...
Regarding the return statement... well ya got me there. Point taken.
Thanks for the reply. Cheers!
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.