Skip to content Skip to sidebar Skip to footer

Create A "converter" Class To Get Metric Measurements

I am developing an app to get user information like weight and height. In according to Locale, the app show two or three EditText: three (pounds, feet, inches) if the Locale is US

Solution 1:

The UnitOf library we just released for Java (Android compatible), JavaScript, and C# is perfect for converting to or from metric and imperial for any unit of measure:

//Mass as one linersdoublekgToLb=newUnitOf.Mass().fromKilograms(5).toPounds();  //11.023122100918888doublelbToKg=newUnitOf.Mass().fromPounds(5).toKilograms();  //2.26796//Length as a stateless variable
UnitOf.Lengthft=newUnitOf.Length().fromFeet(5);
doubleftToCm= ft.toCentimeters();  //152.4doubleftToIn= ft.toInches(); //60

There are over 20 complete units of measure and you never need to know any of the conversion factors! UnitOf can also parse data types, convert to and from fractions, and allows for custom UnitOf measurements to be made.

Solution 2:

You have 2 basic ways:

  1. If you are lazy, try the javax.measure library
  2. If you are not lazy, write your own Converter class:

    publicclassConverter {
    
       publicstaticdoublefeetToCm(double feet){
          return feet * 30.48;
       }
    
       publicstaticdoublepoundsToKg(double pounds){
          return pounds * 0.45359237;
       }
    
       // etc.
    
    }
    

    Usage:

    doublemyPounds=5.5;
    doubemyKilos= Converter.poundsToKg(myPounds);
    

Post a Comment for "Create A "converter" Class To Get Metric Measurements"