当前位置: 首页>>代码示例>>PHP>>正文


PHP Number::getCoefficient方法代码示例

本文整理汇总了PHP中Number::getCoefficient方法的典型用法代码示例。如果您正苦于以下问题:PHP Number::getCoefficient方法的具体用法?PHP Number::getCoefficient怎么用?PHP Number::getCoefficient使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Number的用法示例。


在下文中一共展示了Number::getCoefficient方法的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);
             }
         }
     }
 }
开发者ID:konrness,项目名称:scientific-notation,代码行数:38,代码来源:StandardNotationFormatter.php

示例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();
 }
开发者ID:konrness,项目名称:scientific-notation,代码行数:10,代码来源:ScientificNotationFormatter.php


注:本文中的Number::getCoefficient方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。