-
-
Notifications
You must be signed in to change notification settings - Fork 572
Expand file tree
/
Copy pathWeightconversions.php
More file actions
96 lines (89 loc) · 2.39 KB
/
Copy pathWeightconversions.php
File metadata and controls
96 lines (89 loc) · 2.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
* Class for converting weight between different units.
*/
class WeightConversions
{
/**
* Validates input for conversion methods
*
* @param mixed $value The value to check
* @param string $method The method name for error message
* @throws InvalidArgumentException
*/
private static function validateInput($value, $method)
{
// Make sure we have a numeric value
if (!is_numeric($value)) {
throw new InvalidArgumentException("Invalid input for $method");
}
}
/**
* Converts kilograms to pounds.
*
* @param float $kg The weight in kilograms.
* @return float The equivalent weight in pounds.
* @see https://en.wikipedia.org/wiki/Kilogram
*/
public static function kgToLbs($kg)
{
self::validateInput($kg, 'kgToLbs');
return round($kg * 2.20462, 4);
}
/**
* Converts pounds to kilograms.
*
* @param float $lbs The weight in pounds.
* @return float The equivalent weight in kilograms.
* @see https://en.wikipedia.org/wiki/Pound_(mass)
*/
public static function lbsToKg($lbs)
{
self::validateInput($lbs, 'lbsToKg');
return round($lbs / 2.20462, 4);
}
/**
* Converts grams to kilograms.
*
* @param float $grams The weight in grams.
* @return float The equivalent weight in kilograms.
*/
public static function gToKg($grams)
{
self::validateInput($grams, 'gToKg');
return round($grams / 1000, 4);
}
/**
* Converts kilograms to grams.
*
* @param float $kg The weight in kilograms.
* @return float The equivalent weight in grams.
*/
public static function kgToG($kg)
{
self::validateInput($kg, 'kgToG');
return round($kg * 1000, 4);
}
/**
* Converts ounces to pounds.
*
* @param float $oz The weight in ounces.
* @return float The equivalent weight in pounds.
*/
public static function ozToLbs($oz)
{
self::validateInput($oz, 'ozToLbs');
return round($oz / 16, 4);
}
/**
* Converts pounds to ounces.
*
* @param float $lbs The weight in pounds.
* @return float The equivalent weight in ounces.
*/
public static function lbsToOz($lbs)
{
self::validateInput($lbs, 'lbsToOz');
return round($lbs * 16, 4);
}
}