.00 in a Decimal type.

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: .00 in a Decimal type.

Post 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?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: .00 in a Decimal type.

Post 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"
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: .00 in a Decimal type.

Post by JellyFish »

@Josh: I don't see how number_format solves my 3rd criteria?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: .00 in a Decimal type.

Post by josh »

Isn't that what round() is for?
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: .00 in a Decimal type.

Post by JellyFish »

josh wrote:Isn't that what round() is for?
Isn't what what round is for?
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: .00 in a Decimal type.

Post 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.
Post Reply