By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,120 Members | 1,799 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,120 IT Pros & Developers. It's quick & easy.

Formatting Problem

P: n/a
In a program I'm creating, I'm trying to calculate a percentage. However,
for some reason the calculation is only coming out to 1 decimal place, and I
know there's more. Using this code:

$gshare = $shareresults / $phenresults;
echo "$shareresults / $phenresults = $gshare";

I get the following output:

3,340,000 / 5,180,000 = 0.6

That's wrong. How can I correct this?
Oct 14 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Sat, 14 Oct 2006 17:58:26 -0400, "Smiley" <no**@ofyourbusiness.comwrote:
>In a program I'm creating, I'm trying to calculate a percentage. However,
for some reason the calculation is only coming out to 1 decimal place, and I
know there's more. Using this code:

$gshare = $shareresults / $phenresults;
echo "$shareresults / $phenresults = $gshare";

I get the following output:

3,340,000 / 5,180,000 = 0.6
They're not numbers, not with the commas in them. You can't divide formatted
strings.

When asked to treat strings as numbers, PHP reads as much of the data until it
stops matching basic numeric format, so you're actually getting 3/5 which is
0.6.

Remove the commas; only add them back in again when formatting them for
display.

--
Andy Hassall :: an**@andyh.co.uk :: http://www.andyh.co.uk
http://www.andyhsoftware.co.uk/space :: disk and FTP usage analysis tool
Oct 14 '06 #2

P: n/a
if you're really lazy:

$gshare = str_replace(',','',$shareresults) /
str_replace(',','',$phenresults);

Oct 15 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.