Created
March 5, 2021 08:53
-
-
Save ipcjs/8d461a189392f0baebbdafd5fe9afef5 to your computer and use it in GitHub Desktop.
g2kg/g2lb
This file contains 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
class UnitTool { | |
/* | |
* g to kg | |
*/ | |
public static float g2kg(float g, int divisionValueCode) { | |
float kg = g / 1000.f; | |
float v = gunit_general(kg, divisionValueCode); | |
return v; | |
} | |
/* | |
* g to lb | |
*/ | |
public static float g2lb(float g, int divisionValueCode) { | |
float lb = g * 0.0022046f; | |
float v = gunit_general(lb, divisionValueCode); | |
return v; | |
} | |
private static int getInt(float v) { | |
int v2 = v * 10; | |
int mod = v2 % 10; | |
if (mod >= 9) { | |
return v + 1; | |
} | |
return v; | |
} | |
private static float gunit_general(float v, int divisionValueCode) { | |
switch (divisionValueCode) { | |
case 0: { | |
// 0.01 | |
float v2 = v * 1000; | |
int weight = getInt(v2); | |
weight += 5; | |
weight = weight / 10.0; | |
return (weight / 100.0f); | |
} | |
break; | |
case 1: { | |
// 0.02 | |
float v2 = v * 1000; | |
int weight = getInt(v2); | |
weight += 10; | |
weight = weight / 10.0; | |
if ((weight % 2) != 0) { | |
weight -= 1; | |
} | |
return (weight / 100.0f); | |
} | |
break; | |
case 2: { | |
// 0.05 | |
float v2 = v * 1000; | |
int weight = getInt(v2); | |
weight += 20; | |
weight = weight / 10.0; | |
int mod = weight % 10; | |
if (mod >= 5) { | |
weight = ((int) (weight / 10) * 10) + 5; | |
} else { | |
weight = ((int) (weight / 10) * 10); | |
} | |
return (weight / 100.0f); | |
} | |
break; | |
case 3: { | |
// 0.1 | |
float v2 = v * 100; | |
int weight = getInt(v2); | |
weight = weight + 5; | |
weight = weight / 10.0; | |
return (weight / 10.0f); | |
} | |
break; | |
case 4: { | |
// 0.2 | |
float v2 = v * 100; | |
int weight = getInt(v2); | |
weight += 10; | |
weight = weight / 10.0; | |
if ((weight % 2) != 0) { | |
weight -= 1; | |
} | |
return (weight / 10.0f); | |
} | |
break; | |
default: | |
break; | |
} | |
return v; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment