PHP: get number of decimal digits

前端 未结 18 2267
忘了有多久
忘了有多久 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:06

    You should always be careful about different locales. European locales use a comma for the thousands separator, so the accepted answer would not work. See below for a revised solution:

     function countDecimalsUsingStrchr($stringValue){
            $locale_info = localeconv();
            return strlen(substr(strrchr($stringValue, $locale_info['decimal_point']), 1));
        }
    

    see localeconv

提交回复
热议问题