@@ -17,14 +17,14 @@ discard block |
||
| 17 | 17 | private $grees = [ '元', '拾', '佰', '仟', '万', '拾', '佰', '仟', '亿', '拾', '佰', '仟', '万', '拾', '佰' ]; |
| 18 | 18 | private $thanOne = false; |
| 19 | 19 | |
| 20 | - public function __construct( $money ) { |
|
| 21 | - if ( ! ( is_float( $money ) || is_numeric( $money ) || is_int( $money ) ) ) { |
|
| 22 | - throw new \InvalidArgumentException( $money ); |
|
| 20 | + public function __construct($money) { |
|
| 21 | + if ( ! (is_float($money) || is_numeric($money) || is_int($money))) { |
|
| 22 | + throw new \InvalidArgumentException($money); |
|
| 23 | 23 | } |
| 24 | - if ( $money > 1 ) { |
|
| 24 | + if ($money > 1) { |
|
| 25 | 25 | $this->thanOne = true; |
| 26 | 26 | } |
| 27 | - $this->money = number_format( $money, 2, '.', '' ); |
|
| 27 | + $this->money = number_format($money, 2, '.', ''); |
|
| 28 | 28 | } |
| 29 | 29 | |
| 30 | 30 | /** |
@@ -32,58 +32,58 @@ discard block |
||
| 32 | 32 | * @return string |
| 33 | 33 | */ |
| 34 | 34 | public function toCapital(): string { |
| 35 | - @list( $intPart, $decimalPart ) = explode( '.', $this->money, 2 ); |
|
| 36 | - if ( $this->money == 0 ) { |
|
| 35 | + @list($intPart, $decimalPart) = explode('.', $this->money, 2); |
|
| 36 | + if ($this->money == 0) { |
|
| 37 | 37 | return '零元'; |
| 38 | 38 | } |
| 39 | - $result = $this->getIntPart( $intPart ); |
|
| 40 | - $result .= $this->getDecimalPart( $decimalPart ); |
|
| 39 | + $result = $this->getIntPart($intPart); |
|
| 40 | + $result .= $this->getDecimalPart($decimalPart); |
|
| 41 | 41 | |
| 42 | 42 | return $result; |
| 43 | 43 | } |
| 44 | 44 | |
| 45 | - public function getIntPart( $intPart ) { |
|
| 45 | + public function getIntPart($intPart) { |
|
| 46 | 46 | $result = ''; |
| 47 | - $gree = strlen( $intPart ) - 1; |
|
| 48 | - if ( $intPart > 0 ) { |
|
| 49 | - for ( $i = 0; $i < strlen( $intPart ); $i ++ ) { |
|
| 50 | - $num = $intPart[ $i ]; |
|
| 47 | + $gree = strlen($intPart) - 1; |
|
| 48 | + if ($intPart > 0) { |
|
| 49 | + for ($i = 0; $i < strlen($intPart); $i ++) { |
|
| 50 | + $num = $intPart[ $i ]; |
|
| 51 | 51 | $result .= $this->uppers[ $num ] . $this->grees[ $gree -- ]; |
| 52 | 52 | } |
| 53 | 53 | } |
| 54 | 54 | |
| 55 | - $result = str_replace( '零亿', '亿零', $result ); |
|
| 56 | - $result = str_replace( '零万', '万零', $result ); |
|
| 55 | + $result = str_replace('零亿', '亿零', $result); |
|
| 56 | + $result = str_replace('零万', '万零', $result); |
|
| 57 | 57 | |
| 58 | - $result = str_replace( '零拾', '零', $result ); |
|
| 59 | - $result = str_replace( '零佰', '零', $result ); |
|
| 60 | - $result = str_replace( '零仟', '零', $result ); |
|
| 58 | + $result = str_replace('零拾', '零', $result); |
|
| 59 | + $result = str_replace('零佰', '零', $result); |
|
| 60 | + $result = str_replace('零仟', '零', $result); |
|
| 61 | 61 | |
| 62 | - $result = str_replace( '零零', '零', $result ); |
|
| 63 | - $result = str_replace( '零零', '零', $result ); |
|
| 62 | + $result = str_replace('零零', '零', $result); |
|
| 63 | + $result = str_replace('零零', '零', $result); |
|
| 64 | 64 | |
| 65 | - $result = str_replace( '零亿', '亿', $result ); |
|
| 66 | - $result = str_replace( '零万', '万', $result ); |
|
| 67 | - $result = str_replace( '零元', '元', $result ); |
|
| 65 | + $result = str_replace('零亿', '亿', $result); |
|
| 66 | + $result = str_replace('零万', '万', $result); |
|
| 67 | + $result = str_replace('零元', '元', $result); |
|
| 68 | 68 | |
| 69 | 69 | return $result; |
| 70 | 70 | } |
| 71 | 71 | |
| 72 | - public function getDecimalPart( $decimalPart ) { |
|
| 72 | + public function getDecimalPart($decimalPart) { |
|
| 73 | 73 | $result = ''; |
| 74 | - if ( $decimalPart > 0 ) { |
|
| 74 | + if ($decimalPart > 0) { |
|
| 75 | 75 | //处理小数部分 |
| 76 | - $unit = strlen( $decimalPart ) - 1; |
|
| 77 | - for ( $i = 0; $i < strlen( $decimalPart ); $i ++ ) { |
|
| 78 | - $num = $decimalPart[ $i ]; |
|
| 76 | + $unit = strlen($decimalPart) - 1; |
|
| 77 | + for ($i = 0; $i < strlen($decimalPart); $i ++) { |
|
| 78 | + $num = $decimalPart[ $i ]; |
|
| 79 | 79 | $result .= $this->uppers[ $num ] . $this->units[ $unit -- ]; |
| 80 | 80 | } |
| 81 | 81 | } |
| 82 | - $result = str_replace( '零分', '', $result ); |
|
| 83 | - if ( $this->thanOne ) { |
|
| 84 | - $result = str_replace( '零角', '零', $result ); |
|
| 82 | + $result = str_replace('零分', '', $result); |
|
| 83 | + if ($this->thanOne) { |
|
| 84 | + $result = str_replace('零角', '零', $result); |
|
| 85 | 85 | } else { |
| 86 | - $result = str_replace( '零角', '', $result ); |
|
| 86 | + $result = str_replace('零角', '', $result); |
|
| 87 | 87 | } |
| 88 | 88 | |
| 89 | 89 | return $result; |