Skip to content

Commit 655f569

Browse files
committed
more docs
1 parent 0af7965 commit 655f569

File tree

5 files changed

+239
-6
lines changed

5 files changed

+239
-6
lines changed

docs/volume.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Volume Measurement
2+
3+
Volume 是一個用於計算體積的單位轉換工具,適合用於各種體積單位之間的轉換。
4+
5+
<!-- TOC -->
6+
* [Volume Measurement](#volume-measurement)
7+
* [建立](#建立)
8+
* [可用單位](#可用單位)
9+
* [限縮單位](#限縮單位)
10+
* [轉換](#轉換)
11+
* [格式化](#格式化)
12+
<!-- TOC -->
13+
14+
## 建立
15+
16+
要建立一個 Volume 實例,可以使用以下方法:
17+
18+
```php
19+
use Asika\UnitConverter\Volume;
20+
21+
$volume = new Volume(100); // 100 立方公尺
22+
$volume = new Volume(2, Volume::UNIT_CUBIC_KILOMETERS); // 2 立方公里
23+
24+
$volume = Volume::from(300, 'cm3');
25+
$volume = Volume::from('300 cm3');
26+
```
27+
28+
## 可用單位
29+
30+
- Atom Unit: `fm3`
31+
- Default Unit: `m3`
32+
- Base Unit: `m3`
33+
34+
| 單位 | 常數 | 別名 | 比率(相對 m³) | 說明 |
35+
|-------|--------------------------|-----------------------------------------------|---------------------|------|
36+
| `fm3` | `UNIT_CUBIC_FEMTOMETERS` | `fm^3`, `fm³`, `femtometers3`, `femtometers³` | `1e-45` | 立方飛米 |
37+
| `pm3` | `UNIT_CUBIC_PICOMETERS` | `pm^3`, `pm³`, `picometers3`, `picometers³` | `1e-36` | 立方皮米 |
38+
| `nm3` | `UNIT_CUBIC_NANOMETERS` | `nm^3`, `nm³`, `nanometers3`, `nanometers³` | `1e-27` | 立方奈米 |
39+
| `μm3` | `UNIT_CUBIC_MICROMETERS` | `μm^3`, `um³`, `micrometers3`, `micrometers³` | `1e-18` | 立方微米 |
40+
| `mm3` | `UNIT_CUBIC_MILLIMETERS` | `mm^3`, `mm³`, `millimeters3`, `millimeters³` | `1e-9` | 立方毫米 |
41+
| `cm3` | `UNIT_CUBIC_CENTIMETERS` | `cm^3`, `cm³`, `centimeters3`, `centimeters³` | `1e-6` | 立方公分 |
42+
| `dm3` | `UNIT_CUBIC_DECIMETERS` | `dm^3`, `dm³`, `decimeters3`, `decimeters³` | `1e-3` | 立方分米 |
43+
| `m3` | `UNIT_CUBIC_METERS` | `m^3`, ``, `meters3`, `meters³` | `1` | 立方公尺 |
44+
| `km3` | `UNIT_CUBIC_KILOMETERS` | `km^3`, `km³`, `kilometers3`, `kilometers³` | `1e9` | 立方公里 |
45+
| `in3` | `UNIT_CUBIC_INCHES` | `in^3`, `in³`, `inches3`, `inches³` | `0.000016387064` | 立方英吋 |
46+
| `ft3` | `UNIT_CUBIC_FEET` | `ft^3`, `ft³`, `feet3`, `feet³` | `0.028316846592` | 立方英呎 |
47+
| `yd3` | `UNIT_CUBIC_YARDS` | `yd^3`, `yd³`, `yards3`, `yards³` | `0.764554857984` | 立方碼 |
48+
| `mi3` | `UNIT_CUBIC_MILES` | `mi^3`, `mi³`, `miles3`, `miles³` | `4168181825.440579` | 立方英里 |
49+
| `L` | `UNIT_CUBIC_LITERS` | `l`, `liters`, `liter` | `0.001` | 公升 |
50+
| `gal` | `UNIT_CUBIC_GALLONS` | `gal`, `gallons`, `gallon` | `0.003785411784` | 加侖 |
51+
| `pt` | `UNIT_CUBIC_PINTS` | `pt`, `pints`, `pint` | `0.000473176473` | 品脫 |
52+
| `qt` | `UNIT_CUBIC_QUARTS` | `qt`, `quarts`, `quart` | `0.000946352946` | 夸脫 |
53+
54+
### 限縮單位
55+
56+
由於體積單位的多樣性,Volume 類別提供了 `withOnlyCommonVolumes()` 方法來限制可用的單位,僅保留常用的公制體積單位:
57+
58+
```php
59+
$volume = $volume->withOnlyCommonVolumes();
60+
61+
// OR
62+
63+
$volume = $volume->withAvailableUnits(Volume::UNITS_GROUP_COMMON_VOLUMES);
64+
```
65+
66+
## 轉換
67+
68+
可使用 `to()``toXxx()` 方法將 Volume 轉換成其他單位的值:
69+
70+
```php
71+
$volume->toCubicMeters();
72+
$volume->toCubicKilometers(scale: 4);
73+
$volume->to('ft3');
74+
```
75+
76+
支援的函式
77+
78+
- `toCubicFemtometers()`
79+
- `toCubicPicometers()`
80+
- `toCubicNanometers()`
81+
- `toCubicMicrometers()`
82+
- `toCubicMillimeters()`
83+
- `toCubicCentimeters()`
84+
- `toCubicDecimeters()`
85+
- `toCubicMeters()`
86+
- `toCubicKilometers()`
87+
- `toCubicInches()`
88+
- `toCubicFeet()`
89+
- `toCubicYards()`
90+
- `toCubicMiles()`
91+
- `toCubicLiters()`
92+
- `toCubicGallons()`
93+
- `toCubicPints()`
94+
- `toCubicQuarts()`
95+
96+
## 格式化
97+
98+
可以將體積數值格式化成人類可讀的方式:
99+
100+
```php
101+
$volume = \Asika\UnitConverter\Volume::from(401074580, 'm3')
102+
->withOnlyCommonVolumes();
103+
echo $volume->humanize(divider: ' and '); // 401km3 and 74580m3
104+
```
105+
106+

docs/weight.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Weight Measurement
2+
3+
Weight 是一個用於計算重量的單位轉換工具,適合用於各種重量單位之間的轉換。
4+
5+
<!-- TOC -->
6+
* [Weight Measurement](#weight-measurement)
7+
* [建立](#建立)
8+
* [可用單位](#可用單位)
9+
* [限縮單位](#限縮單位)
10+
* [轉換](#轉換)
11+
* [格式化](#格式化)
12+
<!-- TOC -->
13+
14+
## 建立
15+
16+
要建立一個 Weight 實例,可以使用以下方法:
17+
18+
```php
19+
use Asika\UnitConverter\Weight;
20+
21+
$weight = new Weight(100); // 100 公克
22+
$weight = new Weight(2, Weight::UNIT_KILOGRAMS); // 2 公斤
23+
24+
$weight = Weight::from(300, 'mg');
25+
$weight = Weight::from('300 mg');
26+
```
27+
28+
## 可用單位
29+
30+
- Atom Unit: `fg`
31+
- Default Unit: `g`
32+
- Base Unit: `g`
33+
34+
| 單位 | 常數 | 別名 | 比率(相對 g) | 說明 |
35+
|-------|---------------------------|-------------------------------------------|------------------------|----------|
36+
| `fg` | `UNIT_FEMTOGRAMS` | `femtogram`, `femtograms` | `1e-15` | 飛克 |
37+
| `pg` | `UNIT_PICOGRAMS` | `picogram`, `picograms` | `1e-12` | 皮克 |
38+
| `ng` | `UNIT_NANOGRAMS` | `nanogram`, `nanograms` | `1e-9` | 奈克 |
39+
| `μg` | `UNIT_MICROGRAMS` | `microgram`, `micrograms`, `mcg` | `1e-6` | 微克 |
40+
| `mg` | `UNIT_MILLIGRAMS` | `milligram`, `milligrams` | `1e-3` | 毫克 |
41+
| `g` | `UNIT_GRAMS` | `gram`, `grams` | `1` | 公克 |
42+
| `dg` | `UNIT_DECIGRAMS` | `decigram`, `decigrams` | `0.1` | 分克 |
43+
| `cg` | `UNIT_CENTIGRAMS` | `centigram`, `centigrams` | `0.01` | 釐克 |
44+
| `kg` | `UNIT_KILOGRAMS` | `kilogram`, `kilograms` | `1000` | 公斤 |
45+
| `t` | `UNIT_METRIC_TONS` | `metric ton`, `metric tons`, `tonne`, `tonnes` | `1e6` | 公噸 |
46+
| `oz` | `UNIT_OUNCES` | `ounce`, `ounces` | `28.349523125` | 盎司 |
47+
| `lb` | `UNIT_POUNDS` | `pound`, `pounds` | `453.59237` ||
48+
| `st` | `UNIT_STONES` | `stone`, `stones` | `6350.29318` | 英石 |
49+
| `tn` | `UNIT_TONS` | `ton`, `tons` | `907184.74` | 美噸 |
50+
| `ct` | `UNIT_CARATS` | `carat`, `carats` | `0.2` | 克拉 |
51+
| `N` | `UNIT_NEWTONS` | `newton`, `newtons` | `101.972` | 牛頓 (可變動) |
52+
53+
### 限縮單位
54+
55+
由於重量單位的多樣性,Weight 類別提供了 `withOnlyCommonWeights()` 方法來限制可用的單位,僅保留常用的公制重量單位:
56+
57+
```php
58+
$weight = $weight->withOnlyCommonWeights();
59+
60+
// OR
61+
62+
$weight = $weight->withAvailableUnits(Weight::UNITS_GROUP_COMMON_WEIGHTS);
63+
```
64+
65+
## 轉換
66+
67+
可使用 `to()``toXxx()` 方法將 Weight 轉換成其他單位的值:
68+
69+
```php
70+
$weight->toGrams();
71+
$weight->toKilograms(scale: 4);
72+
$weight->to('lb');
73+
```
74+
75+
支援的函式
76+
77+
- `toFemtograms()`
78+
- `toPicograms()`
79+
- `toNanograms()`
80+
- `toMicrograms()`
81+
- `toMilligrams()`
82+
- `toGrams()`
83+
- `toDecigrams()`
84+
- `toCentigrams()`
85+
- `toKilograms()`
86+
- `toMetricTons()`
87+
- `toOunces()`
88+
- `toPounds()`
89+
- `toStones()`
90+
- `toTons()`
91+
- `toCarats()`
92+
- `toNewtons()`
93+
94+
## 格式化
95+
96+
可以將重量數值格式化成人類可讀的方式:
97+
98+
```php
99+
$weight = \Asika\UnitConverter\Weight::from(12345, 'g')
100+
->withOnlyCommonWeights();
101+
echo $weight->humanize(divider: ' and '); // 12kg and 345g
102+
```
103+
104+
## 重力加速度
105+
106+
由於牛頓 (`N`) 的數值建立在重力加速度上,我們可以替 Weight 實例設定重力加速度的值:
107+
108+
```php
109+
$weight = new \Asika\UnitConverter\Weight(100, 'N');
110+
111+
echo $weight->format(unit: 'kg', scale: 4); // 0.0102kg
112+
113+
$weight = $weight->withGravityAcceleration(1.62); // The gravity acceleration on the Moon
114+
115+
echo $weight->format(unit: 'kg', scale: 4); // 0.0617kg
116+
```

src/Volume.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ class Volume extends AbstractBasicMeasurement
5757
self::UNIT_CUBIC_KILOMETERS,
5858
];
5959

60-
public string $atomUnit = self::UNIT_CUBIC_PICOMETERS;
60+
public string $atomUnit = self::UNIT_CUBIC_FEMTOMETERS;
6161

6262
public string $defaultUnit = self::UNIT_CUBIC_METERS;
6363

src/Weight.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,17 @@ class Weight extends AbstractBasicMeasurement
9696
get {
9797
$units = $this->unitExchanges;
9898
$units[self::UNIT_NEWTONS] = BigDecimal::of(1)
99-
->dividedBy($this->gAcceleration, 3, RoundingMode::HALF_UP)
99+
->dividedBy($this->gravityAcceleration, 3, RoundingMode::HALF_UP)
100100
->toFloat();
101101

102102
return $units;
103103
}
104104
}
105105

106106
// Standard gravity in m/s²
107-
public float $gAcceleration = 9.80665;
107+
public protected(set) BigDecimal $gravityAcceleration {
108+
get => $this->gravityAcceleration ??= BigDecimal::of(9.80665);
109+
}
108110

109111
protected function normalizeUnit(string $unit): string
110112
{
@@ -135,4 +137,11 @@ public function withOnlyCommonWeights(): static
135137
{
136138
return $this->withAvailableUnits(self::UNITS_GROUP_COMMON_WEIGHTS);
137139
}
140+
141+
public function withGravityAcceleration(BigDecimal|float|int|string $gAcceleration): Weight
142+
{
143+
$this->gravityAcceleration = BigDecimal::of($gAcceleration);
144+
145+
return $this;
146+
}
138147
}

tests/test.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
include __DIR__ . '/../vendor/autoload.php';
88

9-
$area = \Asika\UnitConverter\Area::from(401074580, 'm2')
10-
->withOnlyCommonAreas();
11-
echo $area->humanize(); // 401km2 74580m2
9+
$weight = new \Asika\UnitConverter\Weight(100, 'N');
10+
echo $weight->format(unit: 'kg', scale: 4);
11+
echo "\n";
12+
$weight = $weight->withGravityAcceleration(1.62); // The gravity acceleration on the Moon
13+
echo $weight->format(unit: 'kg', scale: 4);

0 commit comments

Comments
 (0)