Page 2 of 2

Re: .00 in a Decimal type.

Posted: Sun Mar 01, 2009 4:09 am
by JellyFish
josh wrote:You'd have to implement this with business logic. I supposed it would do a round to length 2, and then store the field. Float will be fine unless you need to add .00000001 and .00091919 and numbers like that. I would do the rounding in application code but thats just me
Ok, thanks for your response.

Might I also ask, what happens when you add numbers like .00000001, just out of my curiosity?

[EDIT]Making the data type a FLOAT and having php ensure that only 2 digits are allowed does solve my first and second issues. But now all I'm left with is my third issue in my list of criteria, which states that any decimal value should contain two places and no less. Any ideas?

Re: .00 in a Decimal type.

Posted: Sun Mar 01, 2009 5:07 am
by josh
JellyFish wrote:Any ideas?
number_format()

Here's all about pitfalls with floats: http://en.wikipedia.org/wiki/Floating_p ... y_problems
A little googling turned up: http://docs.sun.com/source/806-3568/ncg_goldberg.html

number_format( 8.00 );
: string = "8"
number_format( 8.00, 2 );
: string = "8.00"

Re: .00 in a Decimal type.

Posted: Sat Mar 07, 2009 1:20 pm
by JellyFish
@Josh: I don't see how number_format solves my 3rd criteria?

Re: .00 in a Decimal type.

Posted: Sat Mar 07, 2009 1:27 pm
by josh
Isn't that what round() is for?

Re: .00 in a Decimal type.

Posted: Sat Mar 07, 2009 1:52 pm
by JellyFish
josh wrote:Isn't that what round() is for?
Isn't what what round is for?

Re: .00 in a Decimal type.

Posted: Sun Mar 08, 2009 12:57 am
by josh
Hmm well it sounds like you may need to combine it with string functions. One way would be to see if the last 3 characters are ".00" for number_format()'s return value, or you could round, split the string, call str_pad() and re-concat the string. This is not standard floating point behavior you're definitely going to be working with strings, hope that clears it up.