UnitOf
{{unitOfIs}}



Java
1
UnitOf.{{Uts.pascalCase(measVal())}} unit = new UnitOf.{{Uts.pascalCase(measVal())}}().from{{fromVal()}}( );
2
double x = unit.to{{toVal()}}();  //{{uoValue}}
Download UnitOf_v1.0.0.0.jar & Examples
JavaScript
1
var unit = UnitOf.{{Uts.pascalCase(measVal())}}.from{{fromVal()}}( );
2
var x = unit.to{{toVal()}}();  //{{uoValue}}
Download UnitOf_v1.0.0.0.min.js & Examples
C#
1
UnitOf.{{Uts.pascalCase(measVal())}} unit = new UnitOf.{{Uts.pascalCase(measVal())}}().From{{fromVal()}}( );
2
double x = unit.To{{toVal()}}();  //{{uoValue}}
Download UnitOf_v1.0.0.0.dll & Examples

Easiest and Fastest Measurement & Data Type Conversion Library.

- Available in Java, JavaScript, and C# -




{{category.name}}

  • {{item.text}}

{{UnitOfLength()}} Measurements in UnitOf

- Syntax, Units Available, and Examples -


Quick Summary
In
One Liner
1

Set Then Convert
1
2
3

Data Type and Fractions
1
2
3

Create your own custom measurement
1
UnitOf.Anything x = new UnitOf.Anything("FEET", new HashMap<Object, Double>() {{ put("METERS", 0.3048); put("INCHES", 12.0); }});
var x = UnitOf.Anything("FEET", 1, true, {METERS:0.3048, INCHES:12})//Cust measurement w/ 3 units
UnitOf.Anything x = new UnitOf.Anything("FEET", new Dictionary<Object, Double>(){ {"METERS", 0.3048}, {"INCHES", 12.0} });
2
3


{{unitOfMeasurementToVisibleText(key, $index)}}
Example
In using example
1  
/*
2  
 Units available for our custom UnitOf measurement
3  
*/
4  
{{javaJSOrCS('final String', 'var', 'const String')}} FEET = {{selectedExampleDDL[$index] === '3' ? '1' : '"FEET"'}}; {{selectedExampleDDL[$index] === '3' ? '//Unit name "Feet" with value of 1 also for key' : '//Unit name "Feet" as String.'}}
5  
{{javaJSOrCS('final String', 'var', 'const String')}} METERS = {{selectedExampleDDL[$index] === '3' ? '0.3048' : '"Meters"'}}; {{selectedExampleDDL[$index] === '3' ? '//Unit name/value: 0.3048 Meters in 1 foot.' : '//Unit name "Meters" as String.'}}
6  
{{javaJSOrCS('final double', 'var', 'const double')}} INCHES = 12; {{selectedExampleDDL[$index] === '3' ? '//Unit name/value: 12 inches in 1 foot' : '//Unit name can also be the key conversion value (12 inches in 1 foot).'}}
7  
 
8  
/*
9  
 Create our custom UnitOf measurement and add the units with their values.
10
 The values are defined by how many units it takes to equal one key unit.
11
 Key = 1 foot | values = 12 inches (12 inches in 1 foot), 0.3048 meters (0.3048 meters in 1 foot)
12
*/
13
{{isJS('var','')}}{{bJS('UnitOf.')}}{{bJS('Anything')}} length = {{bJS('new')}} UnitOf.Anything(FEET); //Our key is 1 foot. All added units are their conversion into 1 foot.
14
length.{{langCasing('addUnit')}}(METERS, 0.3048); //Unit name/value: 0.3048 Meters in 1 foot.
15
length.{{langCasing('addUnit')}}(INCHES); //Unit name/value: 12 inches in 1 foot.
13
{{isJS('var','')}}{{bJS('UnitOf.')}}{{bJS('Anything')}} length = {{bJS('new')}} UnitOf.Anything(FEET, 1, true, new {{javaJSOrCS('HashMap', '', 'Dictionary')}}<Object, Double>() {{javaJSOrCS('{', '', '')}}{
14
    {{javaJSOrCS('put(', '', '')}}{{javaJSOrCS('', '', '{')}}METERS: 0.3048{{javaJSOrCS(')','','')}}{{javaJSOrCS(';',',','}, {')}}{{javaJSOrCS(' put(', ' ', '')}}INCHES: INCHES{{javaJSOrCS(')','','')}}{{javaJSOrCS('','','}')}}
15
}{{javaJSOrCS('}','','')}});
13
{{isJS('var','')}}{{bJS('UnitOf.')}}{{bJS('Anything')}} length = {{bJS('new')}} UnitOf.Anything(FEET, {{isJS('[','')}}{{bJS('new ')}}{{bJS('double')}}{{bJS('[]{')}}
14
    METERS, INCHES
15
{{isJS(']','}')}});
16
 
17
/*
18
 One line conversions
19
 No "from" value set yet but can still perform one line conversions by passing in the "from" unit and value as well as the "to" unit.
20
*/
21
{{isJS('var','double')}} from12d5_FeetToMeters = length.{{langCasing('convertNow')}}(12.5, FEET, METERS); //Returns 3.81 as 12.5 feet to meters = 3.81 meters
22
{{isJS('var','double')}} from12d5_FeetToInches = length.{{langCasing('convertNow')}}(12.5, FEET, INCHES); //Returns 150 as 12.5 feet to inches = 150 inches
23
 
24
/*
25
 Setting then converting
26
 The custom UnitOf measurement can define a "from" unit/value so to allow it to be converted more than once.
27
*/
28
length.{{langCasing('from')}}(METERS, 5); //Set the length to 5 Meters
29
{{isJS('var','double')}} from5_MetersToFeet = length.{{langCasing('to')}}(FEET); //Returns 16.404 as 5 meters to feet = ~16.404 feet
30
{{isJS('var','double')}} from5_MetersToInches = length.{{langCasing('to')}}(INCHES); //Returns 196.85 as 5 meters to inches = ~196.85 inches
31
 
32
/*
33
 Returns the currently set values
34
*/
35
{{isJS('var','String')}} keyName = length.{{langCasing('getKeyName')}}(); //Returns "Feet"
36
{{isJS('var','double')}} keyValue = length.{{langCasing('getKeyValue')}}(); //Returns 1
37
{{isJS('var','String')}} fromName = length.{{langCasing('getFromName')}}(); //Returns "Meters"
38
{{isJS('var','double')}} fromValue = length.{{langCasing('getFromValue')}}(); //Returns 5
Example
In using example
1
/**
2
* Convert any {{javaJSOrCS('Java','JavaScript','C#')}} data type and more including converting to and from fractions.
3
*
4
* Default Values:
5
*
6
* When a conversion in DataType fails its default, passed by caller as a parameter or if not uses UnitOf's defaults, is returned.
{{dtcTop}}
* UnitOf defaults (when no parameter is passed) for each method are as follows:
{{dtcTop + 1}}
* toObject(): null
{{dtcTop + 2}}
* toString(): ""
{{dtcTop + 3}}
* toBoolean(): false
{{dtcTop + 4}}
* toChar(): '\u0000'
{{dtcTop + 5}}
* toByte(): (byte)0
{{dtcTop + 6}}
* toShort(): (short)0
{{dtcTop + 7}}
* toInt(): 0
{{dtcTop + 8}}
* toLong(): (long)0
{{dtcTop + 9}}
* toFloat(): 0.0f
{{dtcTop + 10}}
* toDouble(): 0.0d
{{dtcJava}}
* toFraction(): ""
{{dtcJava + 1}}
* toSbyte(): 0
{{dtcJava + 2}}
* toUint(): 0
{{dtcJava + 3}}
* toUlong: 0
{{dtcJava + 4}}
* toUshort: 0
{{dtcJava + 5}}
* toDecimal(): 0.0M
{{dtcJava + 6}}
* toDateTime(): new DateTime()
{{dtcJava + 7}}
* toTimeSpan(): new TimeSpan()
{{dtcTop + 1}}
* toString(): ""
{{dtcTop + 2}}
* toBoolean(): false
{{dtcTop + 3}}
* toNumber(): 0
{{dtcTop + 4}}
* toChar(): ""
{{dtcTop + 5}}
* toInt(): 0
{{dtcTop + 6}}
* toFloat(): 0
{{dtcJS}}
* toFraction(): ""
{{dtLN(dtcJava + 1, dtcJS + 1, dtcCS + 1)}}
*
{{dtLN(dtcJava + 2, dtcJS + 2, dtcCS + 2)}}
* NOTE: {{javaJSOrCS('','','toSbyte, toShort, toUint, toUlong, toUshort, ')}}{{isJS('toNumber, toInt, and toFloat','toByte, toInt, toLong, toFloat, and toDouble')}} {{isJS('functions','methods')}} on failure will attempt to read the value as a fraction and convert it to a decimal form.
{{dtLN(dtcJava + 3, dtcJS + 3, dtcCS + 3)}}
* This is to account for fractions originally passed in as Strings to be converted as a number type (EX: "2/4" {{isJS('toFloat()','toDouble()')}} will return 0.5).
{{dtLN(dtcJava + 4, dtcJS + 4, dtcCS + 4)}}
*/
{{dtLine + 1}}
 
{{dtLine + 2}}
{{isJS('var','String')}} typePassed = {{bJS('new')}} UnitOf.DataType(1.23).{{langCasing('getTypeConstantPassed()')}}; //Returns "{{isJS('number','Double')}}"
{{dtLine + 3}}
{{isJS('var','Object')}} valuePassed = {{bJS('new')}} UnitOf.DataType(1.23).{{langCasing('getValuePassed()')}}; //Returns 1.23
{{dtLine + 4}}
double xToDouble = new UnitOf.DataType("12.5").{{langCasing('t')}}oDouble(); //"12.5" -> 12.5
{{dtLine + 5}}
int xToInt = new UnitOf.DataType(12.5).{{langCasing('t')}}oInt(7); //12.5 -> 12
{{dtLine + 6}}
double xToFloat = new UnitOf.DataType(true).{{langCasing('t')}}oFloat(7.0f); //true -> 7.0
{{dtLine + 7}}
String xToString = new UnitOf.DataType(true).{{langCasing('t')}}oString("If Failed"); //"true" -> true
{{dtLine + 8}}
boolean xToBoolean = new UnitOf.DataType("TrUe").{{langCasing('t')}}oBoolean(); //"TrUe" -> true
{{dtLine + 9}}
boolean xExistsInCollection = new UnitOf.DataType("12.5").{{langCasing('t')}}oBoolean(new Integer[]{5,12,3},false,true); //"12.5" -> true
{{dtLine + 10}}
String xToFraction = new UnitOf.DataType(12.5).{{langCasing('t')}}oFraction(); //12.5 -> "25/2"
{{dtLine + 11}}
String xToReducedFrac = new UnitOf.DataType("2/4").{{langCasing('t')}}oFraction(); //"2/4" -> "1/2"
{{dtLine + 12}}
double fracToDouble = new UnitOf.DataType("2/4").{{langCasing('t')}}oDouble(); //"2/4" -> 0.5
{{dtLine + 13}}
char xToChar = new UnitOf.DataType(12.5).{{langCasing('t')}}oChar('@'); //12.5 -> '1'
{{dtLine + 14}}
Object xToObject = new UnitOf.DataType("12.5").{{langCasing('t')}}oObject(); //"12.5" -> "12.5"
{{dtLine + 15}}
byte xToByte = new UnitOf.DataType(12.5).{{langCasing('t')}}oByte((byte)7); //12.5 -> 12.5
{{dtLine + 16}}
short xToShort = new UnitOf.DataType(12).{{langCasing('t')}}oShort(); //12 -> 12
{{dtLine + 17}}
long xToLong = new UnitOf.DataType(12).{{langCasing('t')}}oLong(); //12 -> 12
{{dtLine + 18}}
sbyte xToSbyte = new UnitOf.DataType(12.5).ToSbyte(); //12.5 -> 12
{{dtLine + 19}}
uint xToUint = new UnitOf.DataType(12.5).ToUint((uint)7); //12.5 -> 12
{{dtLine + 20}}
ulong xToUlong = new UnitOf.DataType(12).ToUlong(); //12 -> 12
{{dtLine + 21}}
ushort xToUshort = new UnitOf.DataType(12).ToUshort(); //12 -> 12
{{dtLine + 22}}
decimal xToDecimal = new UnitOf.DataType(12).ToDecimal(7M); //12 -> 12
{{dtLine + 23}}
DateTime xToDateTime = new UnitOf.DataType("5/2").ToDateTime(); //"5/2/2018 12:00:00 AM"
{{dtLine + 24}}
TimeSpan xToTimeSpan = new UnitOf.DataType(12.5).ToTimeSpan(TimeSpan.FromMinutes(2)); //"00:02:00"
{{dtLine + 4}}
var xToFloat = UnitOf.DataType("12.5").toFloat(); //"12.5" -> 12.5
{{dtLine + 5}}
var xToInt = UnitOf.DataType(12.5).toInt(7); //12.5 -> 12
{{dtLine + 6}}
var xToString = UnitOf.DataType(12).toString(); //12 -> "12"
{{dtLine + 7}}
var xToBoolean = UnitOf.DataType("TrUe").toBoolean(); //"TrUe" -> true
{{dtLine + 8}}
var xExistsInCollection = UnitOf.DataType("12.5").toBoolean([5,12,3],false,true); //"12.5" -> true
{{dtLine + 9}}
var xToFraction = UnitOf.DataType(12.5).toFraction(); //12.5 -> "25/2"
{{dtLine + 10}}
var xToReducedFrac = UnitOf.DataType("2/4").toFraction(); //"2/4" -> "1/2"
{{dtLine + 11}}
var fracToDouble = UnitOf.DataType("2/4").toFloat(); //"2/4" -> 0.5
{{dtLine + 12}}
var xToChar = UnitOf.DataType(12.5).toChar('@'); //12.5 -> '1'
{{dtLine + 13}}
var xToNumber = UnitOf.DataType(12.5).toNumber(7.0); //"12.5" -> 12.5
{{dtLine + 1}}
 
{{dtLine + 2}}
{{bJS('UnitOf.')}}{{bJS('DataType')}}{{isJS('var','')}} x = {{bJS('new')}} UnitOf.DataType("12.5"); //String passed in
{{dtLine + 3}}
double xToDouble = x.{{langCasing('t')}}oDouble(); //"12.5" -> 12.5
{{dtLine + 4}}
int xToInt = x.{{langCasing('t')}}oInt(7); //"12.5" -> 12
{{dtLine + 5}}
double xToFloat = x.{{langCasing('t')}}oFloat(7.0f); //"12.5" -> 12.5
{{dtLine + 6}}
String xToString = x.{{langCasing('t')}}oString("If Failed"); //"12.5" -> "12.5"
{{dtLine + 7}}
boolean xToBoolean = x.{{langCasing('t')}}oBoolean(); //"12.5" -> false
{{dtLine + 8}}
boolean xExistsInCollection = x.{{langCasing('t')}}oBoolean(new Integer[]{5, 12, 3}, false, true); //"12.5" -> true
{{dtLine + 9}}
String xToFraction = x.{{langCasing('t')}}oFraction(); //12.5 -> "25/2"
{{dtLine + 10}}
char xToChar = x.{{langCasing('t')}}oChar('@'); //"12.5" -> '1'
{{dtLine + 11}}
Object xToObject = x.{{langCasing('t')}}oObject(); //"12.5" -> "12.5"
{{dtLine + 12}}
byte xToByte = x.{{langCasing('t')}}oByte((byte)7); //"12.5" -> 12.5
{{dtLine + 13}}
short xToShort = x.{{langCasing('t')}}oShort(); //"12.5" -> 12
{{dtLine + 14}}
long xToLong = x.{{langCasing('t')}}oLong(); //"12.5" -> 12
{{dtLine + 15}}
sbyte xToSbyte = x.ToSbyte(); //"12.5" -> 12
{{dtLine + 16}}
uint xToUint = x.ToUint((uint)7); //"12.5" -> 12
{{dtLine + 17}}
ulong xToUlong = x.ToUlong(); //"12.5" -> 12
{{dtLine + 18}}
ushort xToUshort = x.ToUshort(); //"12.5" -> 12
{{dtLine + 19}}
decimal xToDecimal = x.ToDecimal(7M); //"12.5" -> 12.5
{{dtLine + 20}}
DateTime xToDateTime = x.ToDateTime(); //"12/5/2018 12:00:00 AM"
{{dtLine + 21}}
TimeSpan xToTimeSpan = x.ToTimeSpan(TimeSpan.FromMinutes(2)); //"00:02:00"
{{dtLine + 3}}
var xToFloat = x.toFloat(); //"12.5" -> 12.5
{{dtLine + 4}}
var xToInt = x.toInt(7); //12.5 -> 12
{{dtLine + 5}}
var xToString = x.toString(); //12 -> "12"
{{dtLine + 6}}
var xToBoolean = x.toBoolean(); //"TrUe" -> true
{{dtLine + 7}}
var xExistsInCollection = x.toBoolean([5, 12, 3], false, true); //"12.5" -> true
{{dtLine + 8}}
var xToFraction = x.toFraction(); //12.5 -> "25/2"
{{dtLine + 9}}
var xToReducedFrac = x.toFraction(); //"2/4" -> "1/2"
{{dtLine + 10}}
var fracToDouble = x.toFloat(); //"2/4" -> 0.5
{{dtLine + 11}}
var xToChar = x.toChar('@'); //12.5 -> '1'
{{dtLine + 12}}
var xToNumber = x.toNumber(7.0); //"12.5" -> 12.5
1  
/**
2  
* Normal Syntax (Simple):
3  
*
4  
* Simply parse the value passed.
5  
* An optional single parameter of the default value of true or false can be passed.
6  
* Examples:
7  
* {{bJS('new')}} UnitOf.DataType("true").{{langCasing('toBoolean()')}}; //Returns true
8  
* {{bJS('new')}} UnitOf.DataType("Invalid").{{langCasing('toBoolean()')}}; //Returns false
9  
* {{bJS('new')}} UnitOf.DataType("Invalid").{{langCasing('toBoolean(true)')}}; //Returns true
10
*
11
*-=-=-=-=-=-=-=-=-=-=-=-
12
*
13
* Extended Syntax (Complex):
14
*
15
* Rather than just converting the passed value to true or false as the "Normal Syntax (Simple)" does,
16
* {{langCasing('toBoolean')}} can compare the passed value against a collection of other Objects to find if there match, even of different data types.
17
* When a match exists between the passed object and the passed collection, true is returned. otherwise it is false.
18
* Constraints can be strengthened or loosened by enforcing case sensitivity or matching by containing only part of the value passed.
19
*
20
* UnitOf.{{langCasing('toBoolean()')}} allows passing in a collection of values where is the variable equals any one of them true will be returned.
21
* This can be combined with the other 2 parameters, caseSensitive and contains.
22
* caseSensitive being the UnitOf.DataType variable must match the casing of collection to return true.
23
* EX: caseSensitive = true, "UnitOf" & "unitof" = false
24
* caseSensitive = false, "UnitOf" & "unitof" = true
25
* contains being the collection passed has an element with a sequence of characters that are contains in the UnitOf.DataType variable.
26
* EX: contains = true, "UnitOf" & "Unit" = true
27
* contains = false, "UnitOf" & "tOf" = false
28
* These can be combined something like the following will return true: "uNiTOF" = "UNIt"
29
*/
30
 
31
 
32
/*
33
value used for the next 6 examples
34
*/
35
{{isJS('','UnitOf.')}}{{isJS('','DataType')}}{{isJS('var','')}} x = {{bJS('new')}} UnitOf.DataType("12.5");
36
 
37
//Returns true for all
38
{{isJS('var','boolean')}} t1 = x.{{langCasing('toBoolean(')}}{{bJS('new ')}}{{bJS('String[]')}}{{isJS('[','{')}}"1.2", "12.5", "3"{{isJS(']','}')}}); //True, matches "12.5"
39
{{isJS('var','boolean')}} t2 = x.{{langCasing('toBoolean(')}}{{bJS('new ')}}{{javaJSOrCS('Float[]','','Object[]')}}{{isJS('[','{')}}1.2f, 12.5f, 3f{{isJS(']','}')}}); //True, matches 12.5
40
{{isJS('var','boolean')}} t3 = x.{{langCasing('toBoolean(')}}{{bJS('new ')}}{{javaJSOrCS('Integer[]','','Object[]')}}{{isJS('[','{')}}5, 12, 3{{isJS(']','}')}}, false, true); //True, matches 5 & 12
41
 
42
//Returns false for all
43
{{isJS('var','boolean')}} f1 = x.{{langCasing('toBoolean(')}}{{bJS('new ')}}{{bJS('String[]')}}{{isJS('[','{')}}"1.2", "12.55", "3"{{isJS(']','}')}}); //False, No matches
44
{{isJS('var','boolean')}} f2 = x.{{langCasing('toBoolean(')}}{{bJS('new ')}}{{javaJSOrCS('Float[]','','Object[]')}}{{isJS('[','{')}}1.2f, 12.55f, 3f{{isJS(']','}')}}); //False, No matches
45
{{isJS('var','boolean')}} f3 = x.{{langCasing('toBoolean(')}}{{bJS('new ')}}{{javaJSOrCS('Integer[]','','Object[]')}}{{isJS('[','{')}}5, 12, 3{{isJS(']','}')}}, false, false); //False, No matches
46
 
47
/*
48
One line String examples
49
*/
50
{{isJS('var','boolean')}} noCasingAndContains = {{bJS('new')}} UnitOf.DataType("UnitOf").{{langCasing('toBoolean(')}}{{bJS('new ')}}{{bJS('String[]')}}{{isJS('[','{')}}"Unit", "unitof", "ThisIsUnitOf"{{isJS(']','}')}}, true, true); //Returns true due to "Unit"
51
{{isJS('var','boolean')}} contains = {{bJS('new')}} UnitOf.DataType("UnitOf").{{langCasing('toBoolean(')}}{{bJS('new ')}}{{bJS('String[]')}}{{isJS('[','{')}}"Unit", "unitof", "ThisIsUnitOf"{{isJS(']','}')}},false, true); //Returns true: "Unit", "unitof"
52
{{isJS('var','boolean')}} equals = {{bJS('new')}} UnitOf.DataType("UnitOf").{{langCasing('toBoolean(')}}{{bJS('new ')}}{{bJS('String[]')}}{{isJS('[','{')}}"Unit", "unitof", "ThisIsUnitOf"{{isJS(']','}')}}); //Returns true. only "unitof"
53
{{isJS('var','boolean')}} noCasingAndEquals = {{bJS('new')}} UnitOf.DataType("UnitOf").{{langCasing('toBoolean(')}}{{bJS('new ')}}{{bJS('String[]')}}{{isJS('[','{')}}"Unit", "unitof", "ThisIsUnitOf"{{isJS(']','}')}}, true); //Returns false, no matches
1
/*
2
Decimal to fraction
3
Convert whole numbers, decimals, and numbers in strings to fraction equivalent.
4
*/
5
{{isJS('var','String')}} x1 = {{bJS('new')}} UnitOf.DataType(.5).{{langCasing('toFraction()')}}; //Returns "1/2"
6
{{isJS('var','String')}} x2 = {{bJS('new')}} UnitOf.DataType(.75).{{langCasing('toFraction()')}}; //Returns "3/4"
7
{{isJS('var','String')}} x3 = {{bJS('new')}} UnitOf.DataType("Invalid").{{langCasing('toFraction(')}}"2/3"); //Cannot create a fraction from this, default "2/3" is returned
8
{{isJS('var','String')}} x4 = {{bJS('new')}} UnitOf.DataType("-2").{{langCasing('toFraction()')}}; //Returns "-2" or "-2/1"
9
 
10
/*
11
Fraction to Decimal
12
Convert the following fractions to their decimal ({{isJS('float','double')}}) equivalent.
13
*/
14
{{isJS('var','double')}} y1 = {{bJS('new')}} UnitOf.DataType("2/4").{{javaJSOrCS('toDouble()','toFloat()','ToDouble()')}}; //Returns 0.5
15
{{isJS('var','double')}} y2 = {{bJS('new')}} UnitOf.DataType("Invalid").{{javaJSOrCS('toDouble(','toFloat(','ToDouble(')}}0.25); //Cannot create a fraction from this, default 0.25 is returned
16
{{isJS('var','double')}} y3 = {{bJS('new')}} UnitOf.DataType("4/2").{{javaJSOrCS('toDouble()','toFloat()','ToDouble()')}}; //Returns 2
17
 
18
/*
19
Simplify the following existing fractions.
20
*/
21
{{isJS('var','String')}} z1 = {{bJS('new')}} UnitOf.DataType("2/4").{{langCasing('toFraction()')}}; //Returns "1/2"
22
{{isJS('var','String')}} z2 = {{bJS('new')}} UnitOf.DataType("4/2").{{langCasing('toFraction()')}}; //Returns "2" or "2/1"
23
{{isJS('var','String')}} z3 = {{bJS('new')}} UnitOf.DataType("2/5").{{langCasing('toFraction()')}}; //Returns "2/5", cant get any simpler
24
{{isJS('var','String')}} z4 = {{bJS('new')}} UnitOf.DataType("Invalid").{{langCasing('toFraction(')}}"1/2"); //Cannot create a fraction from this, default "1/2" is returned
Syntax
In using example
* NOTE: For NumericBase to work in JavaScript, a reference to a BigInterger library MUST be made. The following was used in developing/testing UnitOf and is recommended: https://peterolson.github.io/BigInteger.js/BigInteger.min.js
UnitOf.NumericBase currently unsupported in C#. More info here:
1
try {
2
    {{isJS('var','String')}} x = {{bJS('new')}} UnitOf.{{key}}{{bJS('()')}} .{{langCasing(docFromCode)}}() .{{langCasing(docToCode)}}{{bJS('()')}} //{{convertMe(theNumericBaseUserInput, key, docFromCode, docToCode)}}
3
} catch ({{bJS('Exception ')}}e) {
4
    //Handle exception: all NumericBase conversion "To" are throwable. This is due to many conversions having letters passed in that may not apply to other base units. Exceptions caught in this example will be displayed as N/A in the comment above.
5
}
1
try {
2
    {{isJS('var','')}}{{bJS('UnitOf.')}}{{bJS( key)}} unit = {{bJS('new')}} UnitOf.{{key}}{{bJS('()')}} .{{langCasing(docFromCode)}}();
3
    {{isJS('var','String')}} x = unit .{{langCasing(docToCode)}}{{bJS('()')}} //{{convertMe(theNumericBaseUserInput, key, docFromCode, docToCode)}}
4
} catch ({{bJS('Exception ')}}e) {
5
    //Handle exception: all NumericBase conversion "To" are throwable. This is due to many conversions having letters passed in that may not apply to other base units. Exceptions caught in this example will be displayed as N/A in the comment above.
6
}
1
try {
2
    {{isJS('var','')}}{{bJS('UnitOf.')}}{{bJS( key)}} unit = {{bJS('new')}} UnitOf.{{key}}{{bJS('()')}};
3
    unit = unit .{{langCasing(docFromCode)}}();
4
    {{isJS('var','String')}} x = unit .{{langCasing(docToCode)}}{{bJS('()')}} //{{convertMe(theNumericBaseUserInput, key, docFromCode, docToCode)}}
5
} catch ({{bJS('Exception ')}}e) {
6
    //Handle exception: all NumericBase conversion "To" are throwable. This is due to many conversions having letters passed in that may not apply to other base units. Exceptions caught in this example will be displayed as N/A in the comment above.
7
}

Units Available
From
(Initial Value and Unit Type)
To
(Convert into)
{{k.replace('from','')}} {{k.replace("from","")}}
Syntax
In using example
1
{{isJS('var','double')}} x = {{bJS('new')}} UnitOf.{{key}}{{bJS('()')}} .{{langCasing(docFromCode)}}() .{{langCasing(docToCode)}}{{bJS('()')}} //{{convertMe(theUserInput, key, docFromCode, docToCode)}}
1
{{isJS('var','')}}{{bJS('UnitOf.')}}{{bJS( key)}} unit = {{bJS('new')}} UnitOf.{{key}}{{bJS('()')}} .{{langCasing(docFromCode)}}();
2
{{isJS('var','double')}} x = unit .{{langCasing(docToCode)}}{{bJS('()')}} //{{convertMe(theUserInput, key, docFromCode, docToCode)}}
1
{{isJS('var','')}}{{bJS('UnitOf.')}}{{bJS( key)}} unit = {{bJS('new')}} UnitOf.{{key}}{{bJS('()')}};
2
unit = unit .{{langCasing(docFromCode)}}();
3
{{isJS('var','double')}} x = unit .{{langCasing(docToCode)}}{{bJS('()')}} //{{convertMe(theUserInput, key, docFromCode, docToCode)}}

Units Available
From
(Initial Value and Unit Type)
To
(Convert into)
{{k.replace('from','')}} {{k.replace("from","")}}

Palindrome Conversion Algorithm

- How UnitOf converts under the hood -



Behind the scenes, UnitOf is using an in-house developed conversion algorithm, which we believe is the most efficient way to convert units of measurement.
Simply put, almost all conversions are achieved using the defined list of units of the measurement which have all been pre-converted from or into a key unit of the same measurement. More complex conversions like temperature use a set of equations and the inverse functions of those same equations that convert from/into the key unit rather than using a pre-converted list of units. When a conversion is executed, its "from" unit/value and "to" unit are plugged into the this pre-converted list of units (or equations in some cases) which then use basic arithmetic to return a conversion.

In other words, a combination of 2 unique equations are used: The first equation to convert the starting unit/value into the chosen key unit of the same measurement. The second equation to convert this newly converted number into the destination unit. This is all found by knowing how to convert all units into a single chosen (key) unit.


Palindrome Conversion Algorithm summary
Hover to zoom | Click to open in separate page

Frequently Asked Questions



{{faq.question}}

{{faq.answer}}