-1

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
  • two (kilograms, centimeters) in the other cases (France, Italy, Spain, etc)

In both cases, I can get an int value or a String value from each EditText.

I would like to create a Java class that allows me to switch from imperial to metric system (or vice versa) via the two (or three) values.

eldivino87
  • 1,425
  • 1
  • 17
  • 30

2 Answers2

2

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 liners
double kgToLb = new UnitOf.Mass().fromKilograms(5).toPounds();  //11.023122100918888
double lbToKg = new UnitOf.Mass().fromPounds(5).toKilograms();  //2.26796

//Length as a stateless variable
UnitOf.Length ft = new UnitOf.Length().fromFeet(5);
double ftToCm = ft.toCentimeters();  //152.4
double ftToIn = 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.

Digidemic
  • 71
  • 4
  • 2
    Put the info in your user profile, stop linking your site/repo in your answers. –  Jul 12 '18 at 07:01
0

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:

    public class Converter {
    
       public static double feetToCm(double feet) {
          return feet * 30.48;
       }
    
       public static double poundsToKg(double pounds) {
          return pounds * 0.45359237;
       }
    
       // etc.
    
    }
    

    Usage:

    double myPounds = 5.5;
    doube myKilos = Converter.poundsToKg(myPounds);
    
Alex Salauyou
  • 14,185
  • 5
  • 45
  • 67
Nikolas Charalambidis
  • 40,893
  • 16
  • 117
  • 183