Skip to content

Commit

Permalink
Improve string representation of the number (#18)
Browse files Browse the repository at this point in the history
* Rewritten conversion to String

* Fix valueToString function

* Added `useScientificNotation` to all the properties

* Version 1.2.0, updated dependencies
  • Loading branch information
ferraridamiano committed May 4, 2022
1 parent 0418290 commit 81be50f
Show file tree
Hide file tree
Showing 27 changed files with 375 additions and 76 deletions.
34 changes: 22 additions & 12 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.2.0
Improved the formatting of the string representation of the number
(`stringValue`): now it is also possibile to choose between decimal or
scientific notation. Added feet (US survey), feet squared (US survey) and
kilocalories.

# 1.1.1+2
Fix links in `README.md`.

Expand All @@ -8,27 +14,31 @@ Updated `README.md`.
Fix compatibility issue of 1.1.0 with the previous release.

# 1.1.0
In this release we added a complete `CustomConversion` definition. You are not anymore tied to `SimpleCustomConversion`!
Check it out in the updated README. We also simplified all the code!
In this release we added a complete `CustomConversion` definition. You are not
anymore tied to `SimpleCustomConversion`! Check it out in the updated README. We
also simplified all the code!

# 1.0.0
`units_converter` is stable since some releases. In this first 1.x version we made a huge work to improve the algorithm
conversion. Now it is **2x faster** then the previous one and it is simpler (less line of code with known algorithms).
We added more test, the **code coverage is now 100%**.
This release will not break anything, it is compatible with the previous one.
`units_converter` is stable since some releases. In this first 1.x version we
made a huge work to improve the algorithm conversion. Now it is **2x faster**
then the previous one and it is simpler (less line of code with known
algorithms). We added more test, the **code coverage is now 100%**. This release
will not break anything, it is compatible with the previous one.

# 0.4.1
Added hectoPascal. Added a bunch of other test (99% code coverage). Minor bug fix (non-critical).
Added hectoPascal. Added a bunch of other test (99% code coverage). Minor bug
fix (non-critical).

# 0.4.0
Breaking. In this release we used the `lints` package and changed enum to camelCase. We also added minutes/kilometer and
energy foot pounds units.
Breaking. In this release we used the `lints` package and changed enum to
camelCase. We also added minutes/kilometer and energy foot pounds units.

# 0.3.0+1
Added mils.

# 0.3.0
Add support for null safety. Added some test. Added fluid ounces, gill, pennyweight, troy ounce. Bugfix.
Add support for null safety. Added some test. Added fluid ounces, gill,
pennyweight, troy ounce. Bugfix.

# 0.2.0+2
Fix time typo and time symbols
Expand All @@ -37,8 +47,8 @@ Fix time typo and time symbols
Fix force conversion. Fix some comment and README

# 0.2.0
Added Property class. It is the parent of all the properties. In this way we can also remove some code.
Now you can assign a custom name to a Property object.
Added Property class. It is the parent of all the properties. In this way we can
also remove some code. Now you can assign a custom name to a Property object.

# 0.1.0
First release
5 changes: 3 additions & 2 deletions example/example1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@ void main() {
// We get the inches
var unit = length.inches;
// We print the Unit
print('name:${unit.name}, value:${unit.value}, stringValue:${unit.stringValue}, symbol:${unit.symbol}');
}
print(
'name:${unit.name}, value:${unit.value}, stringValue:${unit.stringValue}, symbol:${unit.symbol}');
}
6 changes: 4 additions & 2 deletions example/example2.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ import 'package:units_converter/units_converter.dart';

void main() {
// Initialization of the object
var angle = Angle(significantFigures: 7, removeTrailingZeros: false); // conversion
var angle =
Angle(significantFigures: 7, removeTrailingZeros: false); // conversion
angle.convert(ANGLE.degree, 1);
// We get all the units
var units = angle.getAll(); //We get all ther others units
// Let's print all of them
for (var unit in units) {
print('name:${unit.name}, value:${unit.value}, stringValue:${unit.stringValue}, symbol:${unit.symbol}');
print(
'name:${unit.name}, value:${unit.value}, stringValue:${unit.stringValue}, symbol:${unit.symbol}');
}
}
3 changes: 2 additions & 1 deletion example/example3.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import 'package:units_converter/units_converter.dart';

void main() {
// We give '100' decimal as input
var numeralSystems = NumeralSystems()..convert(NUMERAL_SYSTEMS.decimal, '100');
var numeralSystems = NumeralSystems()
..convert(NUMERAL_SYSTEMS.decimal, '100');
// We get the binary value
print('Binary: ${numeralSystems.binary.stringValue}');
// We get the hexadecimal value
Expand Down
4 changes: 2 additions & 2 deletions example/example4.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:units_converter/units_converter.dart';

void main() {

final Map<String, double> conversionMap = {
'EUR': 1,
'USD': 1.2271,
Expand All @@ -18,7 +17,8 @@ void main() {
'CNY': '¥',
};

var customConversion = SimpleCustomConversion(conversionMap, mapSymbols: mapSymbols);
var customConversion =
SimpleCustomConversion(conversionMap, mapSymbols: mapSymbols);
customConversion.convert('EUR', 1);
Unit usd = customConversion.getUnit('USD');
print('1€ = ${usd.stringValue}${usd.symbol}');
Expand Down
5 changes: 3 additions & 2 deletions example/example5.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ void main() {
mapSymbols: symbolsMap,
name: 'Conversion of Dash',
);

dash.convert('Dash', 1);
var myUnits = dash.getAll();
for (var unit in myUnits) {
print('name:${unit.name}, value:${unit.value}, stringValue:${unit.stringValue}, symbol:${unit.symbol}');
print(
'name:${unit.name}, value:${unit.value}, stringValue:${unit.stringValue}, symbol:${unit.symbol}');
}
}
18 changes: 15 additions & 3 deletions lib/models/custom_conversion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,19 @@ import 'package:units_converter/utils/utils.dart';
class CustomConversion extends Property<dynamic, double> {
//Map between units and its symbol
final Map<dynamic, String?> mapSymbols;

/// The number of significan figures to keep. E.g. 1.23456789) has 9
/// significant figures
int significantFigures;

/// Whether to remove the trailing zeros or not. E.g 1.00000000 has 9
/// significant figures and has trailing zeros. 1 has not trailing zeros.
bool removeTrailingZeros;

/// Whether to use the scientific notation (true) for [stringValue]s or
/// decimal notation (false)
bool useScientificNotation;

final List<Unit> _unitList = [];
late List<Node> _nodeList;
Node conversionTree;
Expand All @@ -23,7 +34,8 @@ class CustomConversion extends Property<dynamic, double> {
required this.mapSymbols,
name,
this.significantFigures = 10,
this.removeTrailingZeros = true}) {
this.removeTrailingZeros = true,
this.useScientificNotation = true}) {
this.name = name;
size = mapSymbols.length;
mapSymbols.forEach((key, value) => _unitList.add(Unit(key, symbol: value)));
Expand All @@ -45,8 +57,8 @@ class CustomConversion extends Property<dynamic, double> {
for (var i = 0; i < mapSymbols.length; i++) {
_unitList[i].value =
_nodeList.singleWhere((node) => node.name == _unitList[i].name).value;
_unitList[i].stringValue = mantissaCorrection(
_unitList[i].value!, significantFigures, removeTrailingZeros);
_unitList[i].stringValue = valueToString(_unitList[i].value!,
significantFigures, removeTrailingZeros, useScientificNotation);
}
}

Expand Down
6 changes: 6 additions & 0 deletions lib/models/simple_custom_conversion.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class SimpleCustomConversion extends Property<dynamic, double> {
/// significant figures and has trailing zeros. 1 has not trailing zeros.
bool removeTrailingZeros;
/// Whether to use the scientific notation (true) for [stringValue]s or
/// decimal notation (false)
bool useScientificNotation;
late CustomConversion _customConversion;
///Class for simple custom conversions. E.g.:
Expand Down Expand Up @@ -54,6 +58,7 @@ class SimpleCustomConversion extends Property<dynamic, double> {
{this.mapSymbols,
this.significantFigures = 10,
this.removeTrailingZeros = true,
this.useScientificNotation = true,
name}) {
assert(mapConversion.containsValue(1),
'One conversion coefficient must be 1, this will considered the base unit');
Expand Down Expand Up @@ -86,6 +91,7 @@ class SimpleCustomConversion extends Property<dynamic, double> {
mapSymbols: mapSymbols!,
significantFigures: significantFigures,
removeTrailingZeros: removeTrailingZeros,
useScientificNotation: useScientificNotation,
name: name ?? PROPERTY.angle);
}
Expand Down
13 changes: 11 additions & 2 deletions lib/properties/angle.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class Angle extends Property<ANGLE, double> {
/// significant figures and has trailing zeros. 1 has not trailing zeros.
bool removeTrailingZeros;

/// Whether to use the scientific notation (true) for [stringValue]s or
/// decimal notation (false)
bool useScientificNotation;

late CustomConversion _customConversion;

///Class for angle conversions, e.g. if you want to convert 1 radiant in degree:
Expand All @@ -36,7 +40,11 @@ class Angle extends Property<ANGLE, double> {
///angle.convert(Unit(ANGLE.radians, value: 1));
///print(ANGLE.degree);
/// ```
Angle({this.significantFigures = 10, this.removeTrailingZeros = true, name}) {
Angle(
{this.significantFigures = 10,
this.removeTrailingZeros = true,
this.useScientificNotation = true,
name}) {
this.name = name ?? PROPERTY.angle;
size = ANGLE.values.length;
Node conversionTree = Node(name: ANGLE.degree, leafNodes: [
Expand All @@ -58,7 +66,8 @@ class Angle extends Property<ANGLE, double> {
conversionTree: conversionTree,
mapSymbols: mapSymbols,
significantFigures: significantFigures,
removeTrailingZeros: removeTrailingZeros);
removeTrailingZeros: removeTrailingZeros,
useScientificNotation: useScientificNotation);
}

/// Converts a unit with a specific name (e.g. ANGLE.degree) and value to all
Expand Down
13 changes: 11 additions & 2 deletions lib/properties/area.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ class Area extends Property<AREA, double> {
/// significant figures and has trailing zeros. 1 has not trailing zeros.
bool removeTrailingZeros;

/// Whether to use the scientific notation (true) for [stringValue]s or
/// decimal notation (false)
bool useScientificNotation;

late CustomConversion _customConversion;

///Class for area conversions, e.g. if you want to convert 1 square meters in
Expand All @@ -53,7 +57,11 @@ class Area extends Property<AREA, double> {
///area.convert(Unit(AREA.square_meters, value: 1));
///print(AREA.acres);
/// ```
Area({this.significantFigures = 10, this.removeTrailingZeros = true, name}) {
Area(
{this.significantFigures = 10,
this.removeTrailingZeros = true,
this.useScientificNotation = true,
name}) {
this.name = name ?? PROPERTY.area;
size = AREA.values.length;
Node conversionTree = Node(name: AREA.squareMeters, leafNodes: [
Expand Down Expand Up @@ -101,7 +109,8 @@ class Area extends Property<AREA, double> {
conversionTree: conversionTree,
mapSymbols: mapSymbols,
significantFigures: significantFigures,
removeTrailingZeros: removeTrailingZeros);
removeTrailingZeros: removeTrailingZeros,
useScientificNotation: useScientificNotation);
}

/// Converts a unit with a specific name (e.g. AREA.hectares) and value to all
Expand Down
12 changes: 10 additions & 2 deletions lib/properties/energy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ class Energy extends Property<ENERGY, double> {
/// significant figures and has trailing zeros. 1 has not trailing zeros.
bool removeTrailingZeros;

/// Whether to use the scientific notation (true) for [stringValue]s or
/// decimal notation (false)
bool useScientificNotation;

late CustomConversion _customConversion;

///Class for energy conversions, e.g. if you want to convert 1 joule in kilowatt hours:
Expand All @@ -41,7 +45,10 @@ class Energy extends Property<ENERGY, double> {
///print(ENERGY.kilowatt_hours);
/// ```
Energy(
{this.significantFigures = 10, this.removeTrailingZeros = true, name}) {
{this.significantFigures = 10,
this.removeTrailingZeros = true,
this.useScientificNotation = true,
name}) {
this.name = name ?? PROPERTY.energy;
size = ENERGY.values.length;
Node conversionTree = Node(name: ENERGY.joules, leafNodes: [
Expand Down Expand Up @@ -73,7 +80,8 @@ class Energy extends Property<ENERGY, double> {
conversionTree: conversionTree,
mapSymbols: mapSymbols,
significantFigures: significantFigures,
removeTrailingZeros: removeTrailingZeros);
removeTrailingZeros: removeTrailingZeros,
useScientificNotation: useScientificNotation);
}

///Converts a unit with a specific name (e.g. ENERGY.calories) and value to all other units
Expand Down
13 changes: 11 additions & 2 deletions lib/properties/force.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class Force extends Property<FORCE, double> {
/// significant figures and has trailing zeros. 1 has not trailing zeros.
bool removeTrailingZeros;

/// Whether to use the scientific notation (true) for [stringValue]s or
/// decimal notation (false)
bool useScientificNotation;

late CustomConversion _customConversion;

///Class for force conversions, e.g. if you want to convert 1 newton in pound force:
Expand All @@ -38,7 +42,11 @@ class Force extends Property<FORCE, double> {
///force.Convert(Unit(FORCE.newton, value: 1));
///print(FORCE.pound_force);
/// ```
Force({this.significantFigures = 10, this.removeTrailingZeros = true, name}) {
Force(
{this.significantFigures = 10,
this.removeTrailingZeros = true,
this.useScientificNotation = true,
name}) {
this.name = name ?? PROPERTY.force;
size = FORCE.values.length;
Node conversionTree = Node(name: FORCE.newton, leafNodes: [
Expand All @@ -64,7 +72,8 @@ class Force extends Property<FORCE, double> {
conversionTree: conversionTree,
mapSymbols: mapSymbols,
significantFigures: significantFigures,
removeTrailingZeros: removeTrailingZeros);
removeTrailingZeros: removeTrailingZeros,
useScientificNotation: useScientificNotation);
}

///Converts a unit with a specific name (e.g. FORCE.newton) and value to all other units
Expand Down
12 changes: 10 additions & 2 deletions lib/properties/fuel_consumption.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class FuelConsumption extends Property<FUEL_CONSUMPTION, double> {
/// significant figures and has trailing zeros. 1 has not trailing zeros.
bool removeTrailingZeros;

/// Whether to use the scientific notation (true) for [stringValue]s or
/// decimal notation (false)
bool useScientificNotation;

late CustomConversion _customConversion;

///Class for fuel_consumption conversions, e.g. if you want to convert 1 kilometers per liter in liters per 100 km:
Expand All @@ -37,7 +41,10 @@ class FuelConsumption extends Property<FUEL_CONSUMPTION, double> {
///print(FUEL_CONSUMPTION.liters_per_100_km);
/// ```
FuelConsumption(
{this.significantFigures = 10, this.removeTrailingZeros = true, name}) {
{this.significantFigures = 10,
this.removeTrailingZeros = true,
this.useScientificNotation = true,
name}) {
this.name = name ?? PROPERTY.fuelConsumption;
size = FUEL_CONSUMPTION.values.length;
Node conversionTree =
Expand All @@ -61,7 +68,8 @@ class FuelConsumption extends Property<FUEL_CONSUMPTION, double> {
conversionTree: conversionTree,
mapSymbols: mapSymbols,
significantFigures: significantFigures,
removeTrailingZeros: removeTrailingZeros);
removeTrailingZeros: removeTrailingZeros,
useScientificNotation: useScientificNotation);
}

///Converts a unit with a specific name (e.g. FUEL_CONSUMPTION.liters_per_100_km) and value to all other units
Expand Down
Loading

0 comments on commit 81be50f

Please sign in to comment.