I think the key problem is determining how many positions are needed. Would you define 13.01 as 13 because the first decimal was 0? Since for the printf and number format you need to know how many decimal places I donโt know this will work for you.
Maybe something like this (it's a lot of functions, but it searches for the first 0, and then returns a truncated string). Yes, it is intense, but it may be the best way for you.
function show_number ($ number, $ max = 8) {
if (strpos ($ number, '.')) {
$ decimal = strpos ($ number, '.');
if (strpos ($ number, '.0')) {
return substr ($ number, 0, $ decimal); // returns whole if zero is first
} else {
if (strpos (substr ($ number, $ decimal, $ max), '0')) {
$ zero = strpos (substr ($ number, $ decimal, $ max), '0');
return substr ($ number, 0, $ decimal + $ zero); // returns number w / 0 first zero
} else {
return substr ($ number, 0, $ decimal + $ max + 1); // returns number with max places
}
}
} else {
return $ number; // returns number if no decimals
}
}
Cryophallion
source share