-1

I am having a problem with number_format. Here is how I am trying to use it:

When I try to multiply it with quantity it returns 1. for example

1,200.00 * 2

It returns 1. How can I make it return 2,400.00?

What I missing? Thanks

Ahmed Fouad
  • 2,963
  • 10
  • 30
  • 54

1 Answers1

10

You cannot multiply formatted numbers. You should do all of your calculations before you format the final result.

lanzz
  • 42,060
  • 10
  • 89
  • 98
  • Can't I remove the formatting? I already did the calculations :( – Ahmed Fouad Oct 28 '12 at 19:01
  • No, you're trying to multiply _after_ you have formatted the number; multiplication is a calculation too, you should do it before you format the number. You _could_ remove the formatting, but that would be inefficient. – lanzz Oct 28 '12 at 19:02
  • So i have to change all session stored prices, db prices to numbers without format, use only format when showing them on screen.. that what you wanna say? – Ahmed Fouad Oct 28 '12 at 19:04
  • 4
    Yes, exactly what I'm saying. You should keep prices in `DECIMAL` or `NUMERIC` columns in your database, and only format them when you actually _need_ to display them formatted. – lanzz Oct 28 '12 at 19:05
  • 1
    @AhmedFouad Hey, you wrote the code. He's trying to help you. – vascowhite Oct 28 '12 at 19:05
  • @AhmedFouad You *can* [unformat money](http://stackoverflow.com/questions/5139793/php-unformat-money/5140046#5140046) but lanzz is actually right here. You should do all calculation *before* you display the formatted string. – Gordon Oct 28 '12 at 19:14