- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 539
Add weight and length conversion functions #183
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Open
      
      
            haj8110
  wants to merge
  3
  commits into
  TheAlgorithms:master
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
haj8110:new-feature-branch
  
      
      
   
  
    
  
  
  
 
  
      
    base: master
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
      
        
          +305
        
        
          −0
        
        
          
        
      
    
  
  
     Open
                    Changes from all commits
      Commits
    
    
  File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| <?php | ||
| /** | ||
| * Class for converting length between different units. | ||
| */ | ||
| class LengthConversions { | ||
| /** | ||
| * 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: expected numeric, got " . gettype($value) . " (" . var_export($value, true) . ")"); | ||
| } | ||
| } | ||
|  | ||
| /** | ||
| * Converts meters to kilometers. | ||
| * | ||
| * @param float $meters The length in meters. | ||
| * @return float The equivalent length in kilometers. | ||
| */ | ||
| public static function mToKm($meters) { | ||
| self::validateInput($meters, 'mToKm'); | ||
| return round($meters / 1000, 4); | ||
| } | ||
|  | ||
| /** | ||
| * Converts kilometers to meters. | ||
| * | ||
| * @param float $kilometers The length in kilometers. | ||
| * @return float The equivalent length in meters. | ||
| */ | ||
| public static function kmToM($kilometers) { | ||
| self::validateInput($kilometers, 'kmToM'); | ||
| return round($kilometers * 1000, 4); | ||
| } | ||
|  | ||
| /** | ||
| * Converts meters to miles. | ||
| * | ||
| * @param float $meters The length in meters. | ||
| * @return float The equivalent length in miles. | ||
| */ | ||
| public static function mToMiles($meters) { | ||
| self::validateInput($meters, 'mToMiles'); | ||
| return round($meters / 1609.34, 6); | ||
| } | ||
|  | ||
| /** | ||
| * Converts miles to meters. | ||
| * | ||
| * @param float $miles The length in miles. | ||
| * @return float The equivalent length in meters. | ||
| */ | ||
| public static function milesToM($miles) { | ||
| self::validateInput($miles, 'milesToM'); | ||
| return round($miles * 1609.34, 4); | ||
| } | ||
|  | ||
| /** | ||
| * Converts inches to centimeters. | ||
| * | ||
| * @param float $inches The length in inches. | ||
| * @return float The equivalent length in centimeters. | ||
| */ | ||
| public static function inToCm($inches) { | ||
| self::validateInput($inches, 'inToCm'); | ||
| return round($inches * 2.54, 4); | ||
| } | ||
|  | ||
| /** | ||
| * Converts centimeters to inches. | ||
| * | ||
| * @param float $centimeters The length in centimeters. | ||
| * @return float The equivalent length in inches. | ||
| */ | ||
| public static function cmToIn($centimeters) { | ||
| self::validateInput($centimeters, 'cmToIn'); | ||
| return round($centimeters / 2.54, 2); | ||
| } | ||
|  | ||
| /** | ||
| * Converts kilometers to miles. | ||
| * | ||
| * @param float $km The length in kilometers. | ||
| * @return float The equivalent length in miles. | ||
| */ | ||
| public static function kmToMiles($km) { | ||
| self::validateInput($km, 'kmToMiles'); | ||
| return round($km / 1.609, 5); | ||
| } | ||
|  | ||
| /** | ||
| * Converts miles to kilometers. | ||
| * | ||
| * @param float $miles The length in miles. | ||
| * @return float The equivalent length in kilometers. | ||
| */ | ||
| public static function milesToKm($miles) { | ||
| self::validateInput($miles, 'milesToKm'); | ||
| return round($miles * 1.609, 4); | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| <?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); | ||
| } | ||
| } | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.