Skip to content

Commit

Permalink
number format fix
Browse files Browse the repository at this point in the history
  • Loading branch information
sjhorn committed Apr 13, 2024
1 parent ea870de commit 0cc666c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
17 changes: 16 additions & 1 deletion lib/src/formula/types/num_type.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import 'package:intl/intl.dart';
import 'package:visicalc_engine/visicalc_engine.dart';

class NumType extends FormulaType {
static const double minExponent = 1.0e-6;
static const double maxExponent = 1.0e21;
final NumberFormat numFormat = NumberFormat('#.############');

NumType(this.value);

final num value;
Expand All @@ -13,8 +18,18 @@ class NumType extends FormulaType {
@override
String toString() => 'Value{$value}';

String get _numString => switch (value.abs()) {
num(isNaN: var nan, isInfinite: var infinite) when nan || infinite =>
'ERROR',
0 => '0',
(>= minExponent && < maxExponent) => numFormat.format(value),
_ => value.toStringAsExponential().length > 11
? value.toStringAsExponential(5)
: value.toStringAsExponential(),
};

@override
String get asFormula => '$value';
String get asFormula => _numString;

@override
void visit(FormulaTypeVisitor callback) {
Expand Down
4 changes: 2 additions & 2 deletions test/visicalc_engine_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ void main() {
'B1'.a1: 'A1+A3*3',
'B2'.a1: '(A1+A3)*3',
'B3'.a1: '12.23e-12',
'B4'.a1: '.23e12',
'B4'.a1: '.12345678901234e-7',
'B5'.a1: 'b4',
'B6'.a1: 'b2',
'B7'.a1: '@sum(b1...b6)',
Expand Down Expand Up @@ -398,7 +398,7 @@ void main() {
expect(engine['A1'.a1]?.resultType, isA<ErrorResult>());
expect(engine['A2'.a1]?.resultType, isA<ErrorResult>());
expect(engine['A3'.a1]?.formulaType?.asFormula, equals('1.223e-11'));
expect(engine['A4'.a1]?.formulaType?.asFormula, equals('230000000000.0'));
expect(engine['A4'.a1]?.formulaType?.asFormula, equals('1.23457e-8'));
expect(engine['A5'.a1]?.formulaType?.asFormula, equals('A4'));
expect(engine['A6'.a1]?.formulaType?.asFormula, equals('A2'));
expect(engine['A7'.a1]?.formulaType?.asFormula, isNull);
Expand Down

0 comments on commit 0cc666c

Please sign in to comment.