Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FORMATTING NUMBERS WITH 2 DECIMALS 1

Status
Not open for further replies.

alexfusion

Programmer
Feb 5, 2001
94
AR
Hello everyone,
Simple problem:
I have a variable that is adding charges,but the result has to contain always 2 decimals.
Let'say:

charges+=0.254444444;

I use the following to get 2 decimals:

charges=Math.round(charges*100)/100;

The problem is when the sum gives a result such as 1 or 1.5.
"zeros" to the right are truncated.
I need always 2 decimals,I mean: 1.00 or 1.50 intead of 1 or 1.5.
Is there a "fast way" of doing this that I don't know? or do I get the lenght of the string after the period and add the "zeros" acording to the result?I think this solution is not very elegant.I mean if the result is 1.5 then I add a "0" to the result,displaying 1.50.
If anyone has some advice,very much appreciated.

Thanks in advance.

alexfusion
 
<script language=&quot;javascript&quot;>
<!--
function numberFormat(str)
{
if(str.indexOf(&quot;.&quot;) == -1)
{
return str;
}

splitString = str.split(&quot;.&quot;);

var newSplitString = splitString[1];

for(i=0; i < (2 - splitString[1].length); i++)
{
newSplitString = newSplitString + &quot;0&quot;;
}

return (splitString[0] + &quot;.&quot; + newSplitString);
}
document.write(numberFormat('2.1'))
-->
</script>
============================
= I'M GONNA RAISE THE BEAST
= TILL I BE HIS FEAST
============================
 
There are a couple of hurdles to negotiate when formatting JavaScript numbers for display. See thread216-271886 ...
 
Can anyone tell me why the idiots who developed javascript couldn't just include a simple printf function like most other languages have?
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Hello everyone,

I'm sorrry it took me some time to reply.
I must thank you for all the responses I got from you.
I was wondering the same question.Why doesn't exists a function to formatting?.
I tought about a similar function that was provided by
pappaobba.

Thanks a lot for the help to all of you.Very much appreciated! :)

Kindest Regards

alexfusion
 
Actually, there are a few standard number formatting functions, for example:
Code:
<html>
<body>
<script>
var x = 1.234;
var n = new Number( x );
document.write( x + &quot;<br>&quot; );
document.write( n.toExponential() + &quot;<br>&quot; );
document.write( n.toFixed( 2 ) + &quot;<br>&quot; );
document.write( n.toLocaleString() + &quot;<br>&quot; );
document.write( n.toPrecision( 2 ) + &quot;<br>&quot; );
</script>
</body>
</html>

What you need is toFixed()

See:

Cheers, Neil :)
 
Hey guys !

I'm back from Albi (south of France) and had a great time there over the long week end (monday was a holliday here in France).

Here is something you can add to your scripts to add rounding :

if (!Number.prototype.toFixed)
{
Number.prototype.toFixed = function(digits)
{
return (Math.round(this*Math.pow(10,digits))/Math.pow(10,digits))
}
}

And then in your script you can simnply call it this way :

alert(Number(1234.1234).toFixed(2))
alert(Number(1234).toFixed(2))
alert(Number(1234).toFixed(5))

Hope this helps. Gary Haran
 
I have a simple function that will round to and always return two decimals:

fRound(Price) {
if (Price>=0) {Price=Price+0.005}
if (Price<0) {Price=Price-0.005}
strPrice=&quot;&quot;+Price
x=strPrice.indexOf(&quot;.&quot;)+3;
strPrice=strPrice.substr(0,x);
return(strPrice);
}

Olav
 
I don't think that any of the following are &quot;standard&quot; functions:

toExponential()
toFixed()
toPrecision()
toLocaleString()

I've never heard of any of them.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
hey Tracy,

It is rather recent and the fix I showed up there was to add the toFixed() method to the number object if it didn't exist yet (older versions).

Check the JavaScript 1.5 documentation if you want to learn more about the toFixed() method.


Also of interest is the JavaScript 2.0 specs. When it comes out it will redefine the web as we know it. ;) Gary Haran
 
Thanks, Gary. I had a feeling that they were new &quot;standard&quot; functions. Meaning, of course, that they really aren't all that standard at all. At least until browsers like NS4.7 go completely out of use.

I like the way you actually added the method to the number object. That's the way to things right.

However I still think a printf function would have been a good idea.
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
yes printf is very powerful indeed. ;)

As a rule of thumb whenever I start developement in JS I ask myself &quot;is there a way to do this by extending the prototype of an existing JS object?&quot;

If you want to see more usage of extensibility like that one that I showed you check out and download the source code to check it out. The project is still a mess and has beta version of a calendar that I am trying to rip out of a bigger project I did.

Even the calendar ( not linked from the home page yet because the code is still in the works) is done by extending the existing JS objects. Gary Haran
 
Tracy
My apologies for saying these functions are standard. I didn't think anyone was still supporting NS4.7 ;-). As you say though, a nice printf or sprintf function would go down a treat. An additional function, which they haven't bothered to get around to is toEngineering(), where numbers are shown to the nearest K,M,G, or T scaling....
Awaiting further modifications... Neil
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top