1: <?php
2: /**
3: * @filesource Kotchasan/Number.php
4: *
5: * @copyright 2016 Goragod.com
6: * @license http://www.kotchasan.com/license/
7: *
8: * @see http://www.kotchasan.com/
9: */
10:
11: namespace Kotchasan;
12:
13: /**
14: * ฟังก์ชั่นตัวเลข
15: *
16: * @author Goragod Wiriya <admin@goragod.com>
17: *
18: * @since 1.0
19: */
20: class Number
21: {
22: /**
23: * ฟังก์ชั่น เติม comma รองรับจุดทศนิยม
24: * ถ้าไม่มีทศนิยมคืนค่า จำนวนเต็ม
25: * ไม่ปัดเศษ
26: *
27: * @assert (100) [==] "100"
28: * @assert (100.1) [==] "100.1"
29: * @assert (1000.12) [==] "1,000.12"
30: * @assert (1000.1555) [==] "1,000.1555"
31: *
32: * @param float $value
33: * @param string $thousands_sep (optional) เครื่องหมายหลักพัน (default ,)
34: *
35: * @return string
36: */
37: public static function format($value, $thousands_sep = ',')
38: {
39: $values = explode('.', $value);
40:
41: return number_format((float) $values[0], 0, '', $thousands_sep).(empty($values[1]) ? '' : '.'.$values[1]);
42: }
43:
44: /**
45: * หังก์ชั่นหาร
46: * $divisor เท่ากับ 0 คืนค่า 0
47: *
48: * @param $actual ตัวตั้ง
49: * @param $divisor ตัวหาร
50: *
51: * @return mixed
52: */
53: public static function division($actual, $divisor)
54: {
55: return $divisor == 0 ? 0 : $actual / $divisor;
56: }
57:
58: /**
59: * จัดรูปแบบตัวเลข รองรับการเติม วัน เดือน ปี
60: *
61: * @assert ('G%04d', 1) [==] "G0001"
62: * @assert ('G-%Y-%m-%d-%04d', 1) [==] "G-63-08-19-0001"
63: * @assert ('G-%y-%m-%d-%04d', 1) [==] "G-20-08-19-0001"
64: * @assert ('G-%YYYY-%m-%d-%04d', 1) [==] "G-2563-08-19-0001"
65: * @assert ('G-%yyyy-%m-%d-%04d', 1) [==] "G-2020-08-19-0001"
66: *
67: * @param string $format
68: * @param mixed $value
69: *
70: * @return string
71: */
72: public static function printf($format, $value)
73: {
74: $year_offset = (int) Language::get('YEAR_OFFSET');
75: $y = date('Y');
76: $Y = $y + $year_offset;
77: $m = date('m');
78: $d = date('d');
79: $format = str_replace(
80: array('%YY', '%yy', '%Y', '%y', '%M', '%m', '%D', '%d'),
81: array($Y, $y, substr($Y, 2, 2), substr($y, 2, 2), $m, $m, $d, $d),
82: $format);
83:
84: return sprintf($format, $value);
85: }
86: }
87: