本文整理汇总了PHP中Number::getExponent方法的典型用法代码示例。如果您正苦于以下问题:PHP Number::getExponent方法的具体用法?PHP Number::getExponent怎么用?PHP Number::getExponent使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Number
的用法示例。
在下文中一共展示了Number::getExponent方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: format
/**
* Returns the simplified standard notation format for the given float, decimal number.
*
* @param Number $number
* @return string
*/
public function format(Number $number)
{
/**
* Move the decimal point to the right for positive exponents of 10.
* Move the decimal point to the left for negative exponents of 10.
*/
$coefficient = (string) $number->getCoefficient();
$decimalPointPosition = strpos($coefficient, ".");
if ($decimalPointPosition === false) {
$decimalPointPosition = 1;
}
$coefficientWithoutDecimal = str_replace(".", "", $coefficient);
$newDecimalPointPosition = $decimalPointPosition + $number->getExponent();
if ($number->getExponent() == 0) {
// no change to be made from coefficient
return (string) $number->getCoefficient();
} else {
if ($number->getExponent() >= 0 && $newDecimalPointPosition - $decimalPointPosition < $number->getSignificantDigits()) {
// move decimal point for number > 1
return substr($coefficientWithoutDecimal, 0, $newDecimalPointPosition) . '.' . substr($coefficientWithoutDecimal, $newDecimalPointPosition);
} else {
if ($newDecimalPointPosition > 0) {
// pad number > 1 with zeros on the right
return str_pad($coefficientWithoutDecimal, $newDecimalPointPosition - $number->getSignificantDigits() + 1, "0", STR_PAD_RIGHT);
} else {
// new decimal point position is less than or equal to zero
// pad number < 1 with zeros on the left
return "0." . str_pad($coefficientWithoutDecimal, abs($newDecimalPointPosition - $number->getSignificantDigits()), "0", STR_PAD_LEFT);
}
}
}
}
示例2: format
/**
* Returns the simplified scientific notation format for the given float, decimal number.
*
* @param Number $number
* @return string
*/
public function format(Number $number)
{
return $number->getCoefficient() . 'x10^' . $number->getExponent();
}