Trim decimal places

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Trim decimal places

Post by ddragas »

I've got number in format 35603.822252
How to leave only two numbers after decimal decimal '.'? (trim or something)

I know that exist 'number_format($number, 2, ’.’, ’.’)' command, but I do not want to use this.

One more thing how to get formating number 35,603.82 to 35.603,82
Last edited by ddragas on Tue Jul 12, 2005 3:59 pm, edited 2 times in total.
User avatar
hawleyjr
BeerMod
Posts: 2170
Joined: Tue Jan 13, 2004 4:58 pm
Location: Jax FL & Spokane WA USA

Re: Trim decimal places

Post by hawleyjr »

ddragas wrote: I know that exist 'number_format($number, 2, ’.’, ’.’)' command, but I do not want to use this.
Why Not?
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Becouse Im using function viewtopic.php?t=33582 (author Burrito) and it is returning text 'Thirty Five Thousand Six Hundred and Three Point Eight Hundred and Twenty Two Thousand Two Hundred and Fifty Two'

So I need to trim all but two decimals
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

So why don't you call number_format() before running ~Burrito's script?
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

If I do it before I get 'Thirty Five Point Eighty Two' (without Hundreds)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Isn't that what you want? Do you want the string representation or do you want the digits themselves?

As for you second question:
PHP Manual wrote:string number_format ( float number [, int decimals [, string dec_point, string thousands_sep]] )
http://www.php.net/number_format
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

No it is not

Number is 35603.822252 and result in text is 'Thirty Five Point Eighty Two' (without Hundreds)
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

The problem is the thousands separater. Call number format like this:

Code: Select all

number_format(35603.822252,2,'.','');
That will remove the thousands separator, and allow ~Burrito's code to work properly.
Last edited by pickle on Tue Jul 12, 2005 5:10 pm, edited 1 time in total.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Shell I ask him to help me repair it? or will you try to help me?
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

I was wrong (sort of) - read my previous post that has now been edited to eliminate all evidence of me being wrong :).
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
ddragas
Forum Contributor
Posts: 445
Joined: Sun Apr 18, 2004 4:01 pm

Post by ddragas »

Thank you

Works great
theda
Forum Contributor
Posts: 332
Joined: Sat Feb 19, 2005 8:35 am
Location: USA

Post by theda »

Oh, and by the way... Using European monetary format is slightly goofy in American programming languages isn't it?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

theda wrote:Oh, and by the way... Using European monetary format is slightly goofy in American programming languages isn't it?
Money was around before programming. It's the programming language that's goofy, not the other way round.
Post Reply