Jump to content

Stupid math problem


NerdConcepts

Recommended Posts

I cannot add say 1,000 and 1,000

 

$total = '1,000' + '1,000';

when you echo $total it comes out '2'

 

yet if I do $total = '1000' + '1000'; it echos out '2000' like it should, one problem. All my pricing values have commas in them. Almost all if not all will always contain a comma and NOT a period. Just wondering how in the check can I remove the comma, add the values, then replace it?

 

BTW, it's for a profit/loss chart. It scans a list a purchased and sold vehicles and does some math and spits out totals. Values are stored as a VARCHAR(20) in the database. Not sure what needs to be done, not sure if storing the numbers different would fix the problem or what.

Link to comment
Share on other sites

The problem is that 1,000 is a string, not a number. Removing the comma will make it a number. The following should work:

 

 

$number1 = "1,000";

$number2 = "1,000";

 

$number1 = str_replace(",", "", $number1);

$number2 = str_replace(",", "", $number2);

 

$total = $number1 + $number2;

 

$total = number_format($total);

 

 

Now when you echo $total, it comes out to 2,000

Link to comment
Share on other sites

  • 1 year later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.