/    Sign up×
Community /Pin to ProfileBookmark

Number formatting

Hello:

I have four columns:
item = xx
unit price = 55.00
quantity = 30
extended price = 1,650.00 (unit price * quantity)

The subtotal (which adds the total column) should appear as 1,650.00. Instead, the number I’m getting is 1.00.

The subtotal number prints correctly up to 999.99. As soon as it reaches a 1,000 and higher as in my case 1,650.00, my number prints 1.00 leaving out the 650 part.

I’m not sure why. I hope someone can help.

Here’s what I did with my code:

First, I set my $subtotal = 0;

Second, for my total price I’m using the following calculation:
$extprice = number_format($price * $qty, 2);

Third, I wrote a calculation which adds the extended price to a subtotal before taxes:
$subtotal = $extprice + $subtotal;

Finally, I want to print the $total which appears as such:

[code]
<tr>
<td colspan=”3″ align=”right”>Subtotal:</td>
<td align=”right”> <?php echo number_format($subtotal, 2); ?></td>
[/code]

Can someone help me out?

Thanks.

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@chucknasty74Aug 07.2005 — the PHP number format function adds commas to numbers for printing, but will not remove them. Use a string function to find and remove them...it looks as though you are putting commas in as you total(in stage 2 you are using format for no reason), which will end up as a 1 as the deliniating comma stops the number parse, add everything as a number, then format. But like I said, I may be missing something.

try using a price like 2.35 w/ change to test and try cutting the formatting out til the very end...
Copy linkTweet thisAlerts:
@summit310authorAug 07.2005 — Hi,

Thanks for the reply.

Basically I want to multiple the price column by the quantity column and then add up that column.

I would like to have the numbers appear with commas in decimals.

How do I format that?
Copy linkTweet thisAlerts:
@bathurst_guyAug 07.2005 — the PHP number format function adds commas to numbers for printing, but will not remove them. Use a string function to find and remove them...it looks as though you are putting commas in as you total(in stage 2 you are using format for no reason), which will end up as a 1 as the deliniating comma stops the number parse, add everything as a number, then format. But like I said, I may be missing something.

try using a price like 2.35 w/ change to test and try cutting the formatting out til the very end...[/QUOTE]

Just so you can read it again and try it.
Copy linkTweet thisAlerts:
@chucknasty74Aug 07.2005 — set $subtotal to 0.0 to make it a float, skip your second step and use:

$extprice = $price * $qty;

at the end use the format to round to two decimal places.
Copy linkTweet thisAlerts:
@summit310authorAug 07.2005 — Hi,

Thanks for the reply.

I modified my script and changed my calculaton of $extprice. I only applied the formatting at the very end. That change did solve the problem.

Is it possible to show the result of price*qty to include decimals and still be able to add up the column? For presentation, a comma would be nice and I'm not sure if you can add the comma and perform the calculation using another formatting technique.

Right now, the result of my $extprice is a whole number - no decimals are appearing.
Copy linkTweet thisAlerts:
@chucknasty74Aug 07.2005 — yes, either use a temp variable and do:

$temp = number_format($colvalue, '2');

print("$temp");

or try:

print(number_format($colvalue, '2'));

as this should leave the actual column value alone.
Copy linkTweet thisAlerts:
@summit310authorAug 07.2005 — Hi,

Thanks for the reply. I figured out the solution. With some insight from all the responses I was able to fix my problem.
×

Success!

Help @summit310 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...