[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: significant figures

As far as I can tell your function is returning a string as well.  If you
call it like this:

        Num = 8.65
        Val = moneyFormat (Num)
        Val = Val +1
        alert (Val)

you get 8.651 -- Val must be a string on return, as the 1 is being
concatented to the previous contents of Val, rather than added.

It usually doesn't matter, though.  The only time you need a number is if
you must do math with it.  I can't think of a reason to convert to string
dollar format until you're ready to output the value, so you just do the
math stuff first, then convert, then show.  The following displays 9.65,
which is correct.

        Num = 8.65
        Num += 1
        Val = moneyFormat (Num)
        alert (Val)

If you have to do more math to the output you can pass it through the
converter again.

It's theoretically possible to get rounding error on floats on any platform,
though I seem to recall the parseFloat stuff is optimized for Unix, so
perhaps the problem is not as prevalent there.  Brendan Eich of Netscape
insists this is not a bug, though (where this issue has been done to death
over the last month on Netscape JavaScript developer's newsgroup), but a
by-product of JavaScript being cross-platform.  The perception of whether or
not this is a bug may impact if and when it gets fixed.  I think that if
it's not considered a bug we need to consider it a PITA.  In which case it
can go on the PITA list (along with full reloading upon resizing), rather
than the bug list.

-- Gordon

At 03:20 PM 3/2/96 -0500, you wrote:
>I have a similar function up at JavaScript 411 which returns a number rather
>than a string, something that is important when dealing with forms and such.  
>Have a look at http://www.his.com/~smithers/freq/beta/dollars.html
>On which platforms does the parseFloat() bug occur?? I know its happening on
>my Win95 machine.
>Anyone else?
>Andy Augustine
>At 10:42 AM 3/2/96 -0800, Gordon McComb wrote:
>>This should take care of most of the issues involved when rounding numbers.
>>function roundDollar (Val) {
>>	Dollar = Math.floor(Val) 
>>	Val = "" + Math.round(Val * 100)
>>	Decimal = Val.substring (Val.length-2, Val.length)
>>	return (Dollar + "." + Decimal);
>>At 02:46 PM 3/2/96 +0000, you wrote:
>>>Hello again
>>>I posted this question over a week ago and have had no replies so I thought
>>>I'd try again!
>>>Is there a way to accurately round off numbers to a given number of decimal
>>>places (2!) - obviously useful for any sort of monetary display field.
>>>I've looked at a fair number of examples of spreadsheets, calculators etc.
>>>and none of them do this!
>>>I've tried using code like
>>>              (round(100 x myNumber))/100
>>>- no good, not only do all the decimal places fill up again but also it's
>>>not accurate! (You typically get something like 1.99999999999 instead of 2
>>>Anyone out there got a definite answer on this one - "it can't be done"
>>>would be better than nothing!

For help about the list, please send a message to 'majordomo@obscure.org'
with the message body 'help'. To unsubscribe, send a message to
'majordomo@obscure.org' with the message body 'unsubscribe javascript'.
List archives and pointer to FAQ: http://www.obscure.org/javascript/