Forget all the rtrims, and regular expressions, coordinates are floats and should be treated as floats, just prepend the variable with (float) to cast it from a string to a float:
$string = "37.422005000000000000000000000000";
echo (float)$string;
output:
37.422005
The actual result you have are floats but passed to you as strings due to the HTTP Protocol, it's good to turn them back into thier natural form to do calculations etc on.
Test case: http://codepad.org/TVb2Xyy3
Note: Regarding the comment about floating point precision in PHP, see this: https://stackoverflow.com/a/3726761/353790
Answer from RobertPitt on Stack OverflowForget all the rtrims, and regular expressions, coordinates are floats and should be treated as floats, just prepend the variable with (float) to cast it from a string to a float:
$string = "37.422005000000000000000000000000";
echo (float)$string;
output:
37.422005
The actual result you have are floats but passed to you as strings due to the HTTP Protocol, it's good to turn them back into thier natural form to do calculations etc on.
Test case: http://codepad.org/TVb2Xyy3
Note: Regarding the comment about floating point precision in PHP, see this: https://stackoverflow.com/a/3726761/353790
Try with rtrim:
$number = rtrim($number, "0");
If you have a decimal number terminating in 0's (e.g. 123.000), you may also want to remove the decimal separator, which may be different depending on the used locale. To remove it reliably, you may do the following:
$number = rtrim($number, "0");
$locale_info = localeconv();
$number = rtrim($number, $locale_info['decimal_point']);
This of course is not a bullet-proof solution, and may not be the best one. If you have a number like 12300.00, it won't remove the trailing 0's from the integer part, but you can adapt it to your specific need.
numbers - Remove useless zero digits from decimals in PHP - Stack Overflow
How would I omit zero value decimals in a number format? - Craft CMS Stack Exchange
magento 1.9 - Remove trailing zeros - Magento Stack Exchange
Remove trailing zeroes
You can add 0 to the formatted string. It will remove trailing zeros.
echo number_format(3.0, 1, ".", "") + 0; // 3
A Better Solution: The above solution fails to work for specific locales. So in that case, you can just type cast the number to float data type. Note: You might loose precision after type casting to float, bigger the number, more the chances of truncating the number.
echo (float) 3.0; // 3
Ultimate Solution: The only safe way is to use regex:
echo preg_replace("/\.?0+$/", "", 3.0); // 3
echo preg_replace("/\d+\.?\d*(\.?0+)/", "", 3.0); // 3
Snippet 1 DEMO
Snippet 2 DEMO
Snippet 3 DEMO
Use:
a = round($a, 2);
Even though the number has 2 zeros trailing it, if you round it, it won't show the decimal places, unless they have some kind of value.
So 50.00 rounded using 2 places will be 50, BUT 50.23 will be 50.23.
Unless you specify at which point to round up or down, it won't change your decimal values. So just use default round()
$num + 0 does the trick.
echo 125.00 + 0; // 125
echo '125.00' + 0; // 125
echo 966.70 + 0; // 966.7
Internally, this is equivalent to casting to float with (float)$num or floatval($num) but I find it simpler.
you could just use the floatval function
echo floatval('125.00');
// 125
echo floatval('966.70');
// 966.7
echo floatval('844.011');
// 844.011
For all those looking for a zeroes-trimming solution for just a single decimal place, please consider the following:
{{ item.width | replace({".0" : ""}) }}
In our case, the previously posted solution will not work reliably, because…
{{ item.width|trim('0')|trim('.') }}
…will correctly trim down 3.0 to 3, but transform 0.3 to 3 just the same. replace(), instead, does work like a charm.
This can be done with Twig's trim filter. It works just like PHP's trim function.
{{ item.width|trim('0','right')|trim('.','right') }}
function parseCurrency($value) {
if ( intval($value) == $value ) {
$return = number_format($value, 0, ".", ",");
}
else {
$return = number_format($value, 2, ".", ",");
/*
If you don't want to remove trailing zeros from decimals,
eg. 19.90 to become: 19.9, remove the next line
*/
$return = rtrim($return, 0);
}
return $return;
}
$prices[] = parseCurrency(1500.00);
$prices[] = parseCurrency(1500.10);
$prices[] = parseCurrency(1500.1);
$prices[] = parseCurrency(1500);
$prices[] = parseCurrency(123.53);
$prices[] = parseCurrency(1224323.53);
$prices[] = parseCurrency(19.99);
print_r($prices);
Outputs:
Array
(
[0] => 1,500
[1] => 1,500.1
[2] => 1,500.1
[3] => 1,500
[4] => 123.53
[5] => 1,224,323.53
[6] => 19.99
)
This inserts commas, rounds to 2 decimal places, removes trailing zeroes, and removes trailing '.':
rtrim(rtrim(number_format($value,2),0),'.')
$money1 = 68.75;
$money2 = 54.35;
$money = $money1 + $money2;
// echo $money will output "123.1";
$formatted = sprintf("%01.2f", $money);
// echo $formatted will output "123.10"
This might help, You can use sprintf given by PHP.
You can use float casting
echo (float) 11.2200;
echo "<br/>";
echo (float) 11.2000;
echo "<br/>";
echo (float) 11.2340;
and you have to check number of digits after decimal point and than get value like below :
$val=(float) 11.2000;
if(strlen(substr(strrchr($val, "."), 1))<2){
echo number_format($val,2);
}
I think this should work:
return preg_replace('/0{1,2}$/', '', $number);
$strings = array ('1.4000', '1.4100', '1.4130', '1.4136', '1.4001', '1.0041');
foreach ($strings as $number) {
echo "$number -> " . preg_replace('/0{0,2}$/', '', $number) . "\n";
}
Produces:
1.4000 -> 1.40
1.4100 -> 1.41
1.4130 -> 1.413
1.4136 -> 1.4136
1.4001 -> 1.4001
1.0041 -> 1.0041
Here's a variation on Barmar's solution that will work even if the input strings don't always have exactly 4 decimal places. It won't add missing zeroes, but it won't strip any more than it should either.
return preg_replace('/(\.[0-9]{2,}?)0*$/', '$1', $number);
As an example:
$strings = array(
'1.4000',
'1.4100',
'1.4130',
'1.4136',
'1.0',
'1.00',
'1.000',
'10000',
);
foreach ($strings as $number) {
$fixed = preg_replace('/(\.[0-9]{2,}?)0*$/', '$1', $number);
echo "{$number} -> {$fixed}\n";
}
Outputs:
1.4000 -> 1.40
1.4100 -> 1.41
1.4130 -> 1.413
1.4136 -> 1.4136
1.0 -> 1.0
1.00 -> 1.00
1.000 -> 1.00
10000 -> 10000