ConversionTool.java 1.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. public class ConversionTool {
  2. public static final float IN_2_CM = 2.54f;
  3. public static final float CM_2_IN = 1 / IN_2_CM;
  4. public static final float FT_2_IN = 12f;
  5. public static final float IN_2_FT = 1 / FT_2_IN;
  6. public static final float M_2_CM = 100f;
  7. public static final float CM_2_M = 1 / M_2_CM;
  8. public static final float KM_2_CM = 100000f;
  9. public static final float CM_2_KM = 1 / KM_2_CM;
  10. public static final float MI_2_IN = 63360f;
  11. public static final float IN_2_MI = 1 / MI_2_IN;
  12. public static float CentimetersToInches(float centimeters){
  13. float inches = 0;
  14. if (centimeters > 0){
  15. inches = centimeters * CM_2_IN;
  16. }
  17. return inches;
  18. }
  19. public static float InchesToCentimeters(float inches){
  20. float centimeters = 0;
  21. if (inches > 0){
  22. centimeters = inches * IN_2_CM;
  23. }
  24. return centimeters;
  25. }
  26. public static float FeetToMeters(float feet){
  27. float meters = 0;
  28. if (feet > 0){
  29. meters = feet * FT_2_IN * IN_2_CM * CM_2_M;
  30. }
  31. return meters;
  32. }
  33. public static float MetersToFeet(float meters){
  34. float feet = 0;
  35. if (meters > 0){
  36. feet = meters * M_2_CM * CM_2_IN * IN_2_FT;
  37. }
  38. return feet;
  39. }
  40. public static float CelsiusToFahrenheit(float celsius){
  41. return celsius * (9f/5f) + 32f;
  42. }
  43. public static float FahrenheitToCelsius(float fahrenheit){
  44. return (fahrenheit - 32f) * (5f/9f);
  45. }
  46. public static float MphToKph(float mph){
  47. float kph = 0;
  48. if (mph > 0){
  49. kph = mph * MI_2_IN * IN_2_CM * CM_2_KM;
  50. }
  51. return kph;
  52. }
  53. public static float KphToMph(float kph){
  54. float mph = 0;
  55. if (kph > 0){
  56. mph = kph * KM_2_CM * CM_2_IN * IN_2_MI;
  57. }
  58. return mph;
  59. }
  60. }