Skip to content

Commit

Permalink
fix calc using delta value by @andrellopes
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiotucano committed Apr 4, 2024
1 parent ae3f564 commit f788aee
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 15 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## 1.0.2+0
* Fix code using delta value by @andrellopes

## 1.0.1+0
* Fix abnt rule

Expand All @@ -21,4 +24,4 @@
* First version of package

## 0.0.1+1
* First version of package
* First version of package by @sergiotucano
28 changes: 19 additions & 9 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,37 @@ import 'package:roundabnt/roundabnt.dart';

/// example how to use the roundabnt package
void main() {

/// call RoundAbnt function
final roundabnt = RoundAbnt();

double resp = 0.00;
resp = roundabnt.roundAbnt(88.241,2); /// return 88.24
print('$resp');
resp = roundabnt.roundAbnt(88.241, 2);

resp = roundabnt.roundAbnt(88.248,2); /// return 88.25
/// return 88.24
print('$resp');

resp = roundabnt.roundAbnt(88.2858,2); /// return 88.29
resp = roundabnt.roundAbnt(88.248, 2);

/// return 88.25
print('$resp');

resp = roundabnt.roundAbnt(88.2358,2); /// return 88.24
resp = roundabnt.roundAbnt(88.2858, 2);

/// return 88.29
print('$resp');

resp = roundabnt.roundAbnt(88.2458,2); /// return 88.25
resp = roundabnt.roundAbnt(88.2358, 2);

/// return 88.24
print('$resp');

resp = roundabnt.roundAbnt(88.2450,2); /// return 88.24
resp = roundabnt.roundAbnt(88.2458, 2);

/// return 88.25
print('$resp');

}
resp = roundabnt.roundAbnt(88.2450, 2);

/// return 88.24
print('$resp');
}
23 changes: 19 additions & 4 deletions lib/src/round_abnt.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:roundabnt/roundabnt.dart';
import 'dart:math' as math;


/// Regra ABNT :
/// Se o proximo número do último algarismo a ser conservado for menor que 5 :
/// manter-se-a o último algarismo a ser conservado.
Expand All @@ -19,20 +18,24 @@ import 'dart:math' as math;
/// https://www.sofazquemsabe.com/2011/01/como-fazer-arredondamento-da-numeracao.html
class RoundAbnt implements RoundAbntImplementation {

//calc rounded number with brazilian abnt rule
@override
double roundAbnt(double aValue, int digits, {double delta = 0.00001}) {
try {
// Check if the value is negative
var negativo = (aValue < 0);

//@andrellopes fix calc using delta value
aValue = aValue.abs() + 0.00000000000001;

// Calculate the power of 10
var pow = math.pow(10, digits.abs());
var intValue = aValue.toInt();
var fracValue = (aValue - intValue).abs();

var powValue = _simpleRoundToEX(fracValue * pow, 12); // Increase precision
var powValue =
_simpleRoundToEX(fracValue * pow, 12); // Increase precision

var intCalc = powValue.toInt();
var fracCalc = ((powValue * 100).toInt()) % 100; // Remove +1

Expand All @@ -41,6 +44,19 @@ class RoundAbnt implements RoundAbntImplementation {
intCalc++;
}

// if (fracCalc > 50) {
// intCalc++;
// } else if (fracCalc == 50) {
// if (intCalc % 2 == 1) {
// intCalc++;
// } else {
// double restPart = (powValue * 10) % 10;
// if (restPart > delta) {
// intCalc++;
// }
// }
// }

// Calculate the final rounded value
var result = (intValue * pow + intCalc) / pow;

Expand All @@ -59,5 +75,4 @@ class RoundAbnt implements RoundAbntImplementation {
var shift = math.pow(10, digits.toDouble());
return (value * shift).roundToDouble() / shift;
}

}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: roundabnt
description: This package allow user to round a number with brazilian abnt rule
version: 1.0.1+0
version: 1.0.2+0
homepage: https://github.com/sergiotucano/roundabnt

environment:
Expand Down

0 comments on commit f788aee

Please sign in to comment.