PHP: get number of decimal digits

前端 未结 18 2268
忘了有多久
忘了有多久 2020-11-28 09:25

Is there a straightforward way of determining the number of decimal places in a(n) integer/double value in PHP? (that is, without using explode)

18条回答
  •  醉梦人生
    2020-11-28 10:09

    If you want readability for the benefit of other devs, locale safe, use:

    function countDecimalPlacesUsingStrrpos($stringValue){
        $locale_info = localeconv();
        $pos = strrpos($stringValue, $locale_info['decimal_point']);
        if ($pos !== false) {
            return strlen($stringValue) - ($pos + 1);
        }
        return 0;
    }
    

    see localeconv

提交回复
热议问题