Lightweight Java library for converting values and doing arithmetic inside the same measurement category.
Supports Java 8 and above.
- Length: metric, imperial, marine, and other common units
- Weight: metric and imperial units
- Temperature: Kelvin, Celsius, Fahrenheit, and Rankine
- Data size: decimal and binary bit/byte units
- Bit rate: decimal and binary bit/byte-per-second units
- Convert between supported units within the same category
- Perform arithmetic operations: addition, subtraction, multiplication, division
- Format values in any desired unit type
Add the dependency to your pom.xml:
<dependency>
<groupId>dev.ercan</groupId>
<artifactId>unit-converter</artifactId>
<version>1.0.1</version>
</dependency>import dev.ercan.unit.converter.length.Length;
import dev.ercan.unit.converter.length.LengthUnit;
Length distance = Length.of(1, LengthUnit.KILOMETER)
.plus(250, LengthUnit.METER)
.minus(10, LengthUnit.FOOT);
String formatted = distance.format(LengthUnit.METER); // "1246.95 m"import dev.ercan.unit.converter.weight.Weight;
import dev.ercan.unit.converter.weight.WeightUnit;
Weight weight = Weight.parse("3 kg")
.plus(250, WeightUnit.GRAM);
String formatted = weight.format(WeightUnit.GRAM); // "3250 gr"import dev.ercan.unit.converter.temperature.Temperature;
import dev.ercan.unit.converter.temperature.TemperatureUnit;
Temperature temperature = Temperature.ofCelsius(20)
.plus(18, TemperatureUnit.FAHRENHEIT);
String celsius = temperature.format(TemperatureUnit.CELSIUS); // "30 °C"import dev.ercan.unit.converter.data.size.DataSize;
import dev.ercan.unit.converter.data.size.DataSizeUnit;
DataSize size = DataSize.of(2, DataSizeUnit.MEBIBYTE);
String formatted = size.format(DataSizeUnit.KIBIBYTE); // "2048 KiB"import dev.ercan.unit.converter.data.transfer.BitRate;
import dev.ercan.unit.converter.data.transfer.BitRateUnit;
BitRate bitRate = BitRate.of(100, BitRateUnit.MEBIBIT)
.multipliedBy(4)
.dividedBy(2);
String formatted = bitRate.format(BitRateUnit.MEBIBIT); // "200 Mibps"Contributions are welcome! Feel free to fork the repo, submit pull requests or open issues.
This project is licensed under the MIT License.