Re: Preserving Decimals

From: Date: Fri, 16 Jun 2000 22:44:39 +0000
Subject: Re: Preserving Decimals
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-2106@lists.php.net to get a copy of this message
At 05:05 PM 6/16/00 -0500, Yoed Anis wrote:
Thanks a lot Zak! This helped me correct the problem. Finally after hours..... ;-)
Hi Yeod, I am very glad to help! When I first ran into the same problem a bit over 2 years ago, I had a devil of a time with it. Instead of figuring out how to use s/printf, I ended up coming up with a solution like this. :) function OO ($number) {
        list ($integer, $remainder) = explode ('.', $number);
        switch ( strlen ($remainder))
        {
                case 0:
                        return $number . '.00';
                break;
                case 1:
                        return $number . '0';
                break;
                default:
                        return $integer . '.' . round ("0.$remainder" * 100);
        }
} When I stumbled across my code again about a year after the project, it took me a few minutes to figure what it did. I was calling it after every mathematical operation for all of my calculations with dollar amount! This was one of my first difficult programming projects and I guess that the 0 truncating behavior must have had me pretty spooked. The funniest thing is that I called it OO because one of its primary functions was adding trailing 0s for numeric values. Um... yeah, that is my pointless anecdote for the day - I hope someone else finds it funny! :) Ciao, Zak Greant Creative Director Nucleus Information Service Inc. "I am rather like a mosquito in a nudist camp: I know what I ought to do, but I don't know where to begin." - Stephen Bayne

« previous php.general (#2106) next »