# Thread: Mathematical problems when using number_format()

1. ## 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. Yes. Do the math on the numbers, THEN format them into strings for output.

3. 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. Do the number_format at output time, don't touch the data itself.

PHP Code:
``` echo number_format(\$val, 2, '.', ',');  ```
not

PHP Code:
``` \$val = number_format(\$val, 2, '.', ',');echo \$val;  ```

5. 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. 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. 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(\$value, 2);//eval a template "<p>\$output</p>"}  ```

8. eval() almost never belongs in PHP code to begin with :/

9. eval() almost never belongs in PHP code to begin with :/
What do you mean?

#### Posting Permissions

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