Toyed with a unit conversion and string creator
This commit is contained in:
parent
bd07aca95d
commit
5c1d3bdb48
3 changed files with 110 additions and 0 deletions
|
@ -39,4 +39,16 @@ public enum ImperialUnits
|
|||
this.toFeet = toFeet;
|
||||
}
|
||||
|
||||
public static float convert(ImperialUnits a, ImperialUnits b, float value)
|
||||
{
|
||||
value *= a.toFeet;
|
||||
value /= b.toFeet;
|
||||
return value;
|
||||
}
|
||||
|
||||
public float convert(ImperialUnits unit, float value)
|
||||
{
|
||||
return ImperialUnits.convert(this, unit, value);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -61,6 +61,18 @@ public enum MetricUnit
|
|||
return value < this.value;
|
||||
}
|
||||
|
||||
public static float convert(MetricUnit a, MetricUnit b, float value)
|
||||
{
|
||||
value *= a.value;
|
||||
value /= b.value;
|
||||
return value;
|
||||
}
|
||||
|
||||
public float convert(MetricUnit unit, float value)
|
||||
{
|
||||
return MetricUnit.convert(this, unit, value);
|
||||
}
|
||||
|
||||
public static String applyUnits(float value, int decimalPlaces, float multiplier)
|
||||
{
|
||||
String prefix = "";
|
||||
|
|
86
src/com/builtbroken/common/science/units/UnitHelper.java
Normal file
86
src/com/builtbroken/common/science/units/UnitHelper.java
Normal file
|
@ -0,0 +1,86 @@
|
|||
package com.builtbroken.common.science.units;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.builtbroken.common.Pair;
|
||||
|
||||
public class UnitHelper
|
||||
{
|
||||
public static final float FEET_TO_METERS = 0.3048f;
|
||||
public static final float METERS_TO_FEET = 3.28084f;
|
||||
|
||||
public static List<Character> numbers = new ArrayList<>();
|
||||
|
||||
static
|
||||
{
|
||||
numbers.add('0');
|
||||
numbers.add('1');
|
||||
numbers.add('2');
|
||||
numbers.add('3');
|
||||
numbers.add('4');
|
||||
numbers.add('5');
|
||||
numbers.add('6');
|
||||
numbers.add('7');
|
||||
numbers.add('8');
|
||||
numbers.add('9');
|
||||
}
|
||||
|
||||
public float convert(ImperialUnits a, MetricUnit b, float value)
|
||||
{
|
||||
return b.convert(MetricUnit.BASE, a.convert(ImperialUnits.foot, value) * FEET_TO_METERS);
|
||||
|
||||
}
|
||||
|
||||
public float convert(MetricUnit a, ImperialUnits b, float value)
|
||||
{
|
||||
return b.convert(ImperialUnits.foot, a.convert(MetricUnit.BASE, value) * METERS_TO_FEET);
|
||||
}
|
||||
|
||||
public Pair<Object, Float> parseString(String input)
|
||||
{
|
||||
Pair<Object, Float> def = null;
|
||||
if (input != null && !input.isEmpty())
|
||||
{
|
||||
String editedString = input;
|
||||
char[] chars = input.toCharArray();
|
||||
String number = "";
|
||||
String units = "";
|
||||
int toPowerOf = 1;
|
||||
int timeTenToPowerOF = 1;
|
||||
|
||||
//Get number first
|
||||
for (int i = 0; i < chars.length; i++)
|
||||
{
|
||||
char c = chars[i];
|
||||
if (numbers.contains(c))
|
||||
{
|
||||
number += c;
|
||||
}
|
||||
else
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
editedString.replaceAll("[0-9]", "");
|
||||
chars = editedString.toCharArray();
|
||||
//Check if number is being times by 10 to the power of something
|
||||
if (chars != null)
|
||||
{
|
||||
if (chars.length >= 5 && chars[0] == 'x' && chars[1] == '1' && chars[2] == '0' && chars[3] == '^' && numbers.contains(chars[4]))
|
||||
{
|
||||
timeTenToPowerOF = Integer.parseInt("" + chars[4], 1);
|
||||
editedString = editedString.substring(5);
|
||||
}
|
||||
else if (chars.length >= 2 && chars[0] == '^' && numbers.contains(chars[1]))
|
||||
{
|
||||
toPowerOf = Integer.parseInt("" + chars[1], 1);
|
||||
editedString = editedString.substring(2);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
return def;
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue