Skip to main content
added 580 characters in body
Source Link
Mark Baker
  • 213k
  • 34
  • 354
  • 390

Don't add values that have been formatted using number_format(), otherwise they may well contain , as a thousand's separator... they're treated as strings rather than floats.

Assuming that $cash, $card and $check are arrays:

$cash = array_sum($cash); $card = array_sum($card); $check = array_sum($check); $total = number_format( $cash + $card + $check, 2 ); $cash = number_format( $cash, 2 ); $card = number_format( $card, 2 ); $check = number_format( $check, 2 ); 

Do any formatting after the math

EDIT

Use str_replace() rather than preg_replace() to strip out any $, then it's simple addition rather than needing to do the array_sum(), though I'm not sure where any $ is likely to come from if the values are DECIMAL 10,7 from the database

$cash = (float) str_replace( array('$',','), '', $cash ); $card = (float) str_replace( array('$',','), '', $card ); $check = (float) str_replace( array('$',','), '', $check ); $total = number_format( $cash + $card + $check, 2 ); $cash = number_format( $cash, 2 ); $card = number_format( $card, 2 ); $check = number_format( $check, 2 ); 

(assumes that , is your thousands separator in the string values for $cash, $card and $check when doing the str_replace()

EDIT 2

To convert the $cash array to a sum of its values, having cleaned up any $ or , in the strings:

$cash = array ( 0 => '$1729.13', 1 => '0.00', 2 => '$1,234.56' ); function cleanArrayValues($value) { return (float) str_replace( array('$',','), '', $value ); } $cash = array_sum(array_map('cleanArrayValues',$cash)); 

I've added an extra value into the $cash array just to demonstrate.

You'll need to filter $card and $checks in the same way, using the cleanArrayValues() callback function (if they're all arrays)

Don't add values that have been formatted using number_format(), otherwise they may well contain , as a thousand's separator... they're treated as strings rather than floats.

Assuming that $cash, $card and $check are arrays:

$cash = array_sum($cash); $card = array_sum($card); $check = array_sum($check); $total = number_format( $cash + $card + $check, 2 ); $cash = number_format( $cash, 2 ); $card = number_format( $card, 2 ); $check = number_format( $check, 2 ); 

Do any formatting after the math

EDIT

Use str_replace() rather than preg_replace() to strip out any $, then it's simple addition rather than needing to do the array_sum(), though I'm not sure where any $ is likely to come from if the values are DECIMAL 10,7 from the database

$cash = (float) str_replace( array('$',','), '', $cash ); $card = (float) str_replace( array('$',','), '', $card ); $check = (float) str_replace( array('$',','), '', $check ); $total = number_format( $cash + $card + $check, 2 ); $cash = number_format( $cash, 2 ); $card = number_format( $card, 2 ); $check = number_format( $check, 2 ); 

(assumes that , is your thousands separator in the string values for $cash, $card and $check when doing the str_replace()

Don't add values that have been formatted using number_format(), otherwise they may well contain , as a thousand's separator... they're treated as strings rather than floats.

Assuming that $cash, $card and $check are arrays:

$cash = array_sum($cash); $card = array_sum($card); $check = array_sum($check); $total = number_format( $cash + $card + $check, 2 ); $cash = number_format( $cash, 2 ); $card = number_format( $card, 2 ); $check = number_format( $check, 2 ); 

Do any formatting after the math

EDIT

Use str_replace() rather than preg_replace() to strip out any $, then it's simple addition rather than needing to do the array_sum(), though I'm not sure where any $ is likely to come from if the values are DECIMAL 10,7 from the database

$cash = (float) str_replace( array('$',','), '', $cash ); $card = (float) str_replace( array('$',','), '', $card ); $check = (float) str_replace( array('$',','), '', $check ); $total = number_format( $cash + $card + $check, 2 ); $cash = number_format( $cash, 2 ); $card = number_format( $card, 2 ); $check = number_format( $check, 2 ); 

(assumes that , is your thousands separator in the string values for $cash, $card and $check when doing the str_replace()

EDIT 2

To convert the $cash array to a sum of its values, having cleaned up any $ or , in the strings:

$cash = array ( 0 => '$1729.13', 1 => '0.00', 2 => '$1,234.56' ); function cleanArrayValues($value) { return (float) str_replace( array('$',','), '', $value ); } $cash = array_sum(array_map('cleanArrayValues',$cash)); 

I've added an extra value into the $cash array just to demonstrate.

You'll need to filter $card and $checks in the same way, using the cleanArrayValues() callback function (if they're all arrays)

added 508 characters in body
Source Link
Mark Baker
  • 213k
  • 34
  • 354
  • 390

Don't add values that have been formatted using number_format(), otherwise they may well contain , as a thousand's separator... they're treated as strings rather than floats.

Assuming that $cash, $card and $check are arrays:

$cash = array_sum($cash); $card = array_sum($card); $check = array_sum($check); $total = number_format( $cash + $card + $check, 2 ); $cash = number_format( $cash, 2 ); $card = number_format( $card, 2 ); $check = number_format( $check, 2 ); 

Do any formatting after the math

EDIT

Use str_replace() rather than preg_replace() to strip out any $, then it's simple addition rather than needing to do the array_sum(), though I'm not sure where any $ is likely to come from if the values are DECIMAL 10,7 from the database

$cash = (float) str_replace( array('$',','), '', $cash ); $card = (float) str_replace( array('$',','), '', $card ); $check = (float) str_replace( array('$',','), '', $check ); $total = number_format( $cash + $card + $check, 2 ); $cash = number_format( $cash, 2 ); $card = number_format( $card, 2 ); $check = number_format( $check, 2 ); 

(assumes that , is your thousands separator in the string values for $cash, $card and $check when doing the str_replace()

Don't add values that have been formatted using number_format(), otherwise they may well contain , as a thousand's separator... they're treated as strings rather than floats.

Assuming that $cash, $card and $check are arrays:

$cash = array_sum($cash); $card = array_sum($card); $check = array_sum($check); $total = number_format( $cash + $card + $check, 2 ); $cash = number_format( $cash, 2 ); $card = number_format( $card, 2 ); $check = number_format( $check, 2 ); 

Do any formatting after the math

EDIT

Use str_replace() rather than preg_replace() to strip out any $, then it's simple addition rather than needing to do the array_sum(), though I'm not sure where any $ is likely to come from if the values are DECIMAL 10,7 from the database

Don't add values that have been formatted using number_format(), otherwise they may well contain , as a thousand's separator... they're treated as strings rather than floats.

Assuming that $cash, $card and $check are arrays:

$cash = array_sum($cash); $card = array_sum($card); $check = array_sum($check); $total = number_format( $cash + $card + $check, 2 ); $cash = number_format( $cash, 2 ); $card = number_format( $card, 2 ); $check = number_format( $check, 2 ); 

Do any formatting after the math

EDIT

Use str_replace() rather than preg_replace() to strip out any $, then it's simple addition rather than needing to do the array_sum(), though I'm not sure where any $ is likely to come from if the values are DECIMAL 10,7 from the database

$cash = (float) str_replace( array('$',','), '', $cash ); $card = (float) str_replace( array('$',','), '', $card ); $check = (float) str_replace( array('$',','), '', $check ); $total = number_format( $cash + $card + $check, 2 ); $cash = number_format( $cash, 2 ); $card = number_format( $card, 2 ); $check = number_format( $check, 2 ); 

(assumes that , is your thousands separator in the string values for $cash, $card and $check when doing the str_replace()

added 117 characters in body
Source Link
Mark Baker
  • 213k
  • 34
  • 354
  • 390

Don't add values that have been formatted using number_format(), otherwise they may well contain , as a thousand's separator... they're treated as strings rather than floats.

Assuming that $cash, $card and $check are arrays:

$cash = array_sum($cash); $card = array_sum($card); $check = array_sum($check); $total = number_format( $cash + $card + $check, 2 ); $cash = number_format( $cash, 2 ); $card = number_format( $card, 2 ); $check = number_format( $check, 2 ); 

Do any formatting after the math

EDIT

Use str_replace() rather than preg_replace() to strip out any $, then it's simple addition rather than needing to do the array_sum(), though I'm not sure where any $ is likely to come from if the values are DECIMAL 10,7 from the database

Don't add values that have been formatted using number_format(), otherwise they may well contain , as a thousand's separator... they're treated as strings rather than floats.

Assuming that $cash, $card and $check are arrays:

$cash = array_sum($cash); $card = array_sum($card); $check = array_sum($check); $total = number_format( $cash + $card + $check, 2 ); $cash = number_format( $cash, 2 ); $card = number_format( $card, 2 ); $check = number_format( $check, 2 ); 

Do any formatting after the math

Use str_replace() rather than preg_replace() to strip out any $, then it's simple addition rather than needing to do the array_sum()

Don't add values that have been formatted using number_format(), otherwise they may well contain , as a thousand's separator... they're treated as strings rather than floats.

Assuming that $cash, $card and $check are arrays:

$cash = array_sum($cash); $card = array_sum($card); $check = array_sum($check); $total = number_format( $cash + $card + $check, 2 ); $cash = number_format( $cash, 2 ); $card = number_format( $card, 2 ); $check = number_format( $check, 2 ); 

Do any formatting after the math

EDIT

Use str_replace() rather than preg_replace() to strip out any $, then it's simple addition rather than needing to do the array_sum(), though I'm not sure where any $ is likely to come from if the values are DECIMAL 10,7 from the database

Source Link
Mark Baker
  • 213k
  • 34
  • 354
  • 390
Loading