SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Mathematical problems when using number_format()

    Every time i use number_format() i can't do proper mathematical operations (ex. array_sum()) to the formatted numbers after that.

    Is there a way to go around that?

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Yes. Do the math on the numbers, THEN format them into strings for output.

  3. #3
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes. Do the math on the numbers, THEN format them into strings for output.
    I thought of this.. but I was looking for something that doesn't mess the calculations even if we format the numbers, because I have outputs at some places, then again the same numbers used in a calculation code below and then outputted etc.... and i'm not looking to do 2 separate arrays..

  4. #4
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Do the number_format at output time, don't touch the data itself.

    PHP Code:
    echo number_format($val2'.'','); 
    not

    PHP Code:
    $val number_format($val2'.'',');
    echo 
    $val

  5. #5
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do the number_format at output time, don't touch the data itself.
    But my output is based on templates.. so the [numbers] variables are in HTML templates, and I eval() these templates, after retrieving them in my file.. so i can't do something like

    PHP Code:
    echo "The number is ".number_format($number); 

  6. #6
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Assign the formatted value to the template variable, without changing the values you're actually using.

    If you can't make this work, you're using a bad templating system. You should be able to separate data and business logic from display logic.

    It may seem like a pain, but this kinda problem is the reason why.

  7. #7
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Assign the formatted value to the template variable, without changing the values you're actually using.

    If you can't make this work, you're using a bad templating system. You should be able to separate data and business logic from display logic.

    It may seem like a pain, but this kinda problem is the reason why.
    Mybe I'm not seperating both..

    Because for example when eval a template while looping using foreach i use $value in the template so that it gets evaled directly without having to make something like $output_value=$value;

    PHP Code:
    foreach($array as $key => $value) {
    //eval a template "<p>$value</p>"

    so you recommend doing something like the following?
    PHP Code:
    foreach($array as $key => $value) {
    $output number_format($value2);
    //eval a template "<p>$output</p>"


  8. #8
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    eval() almost never belongs in PHP code to begin with :/

  9. #9
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    eval() almost never belongs in PHP code to begin with :/
    What do you mean?


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •