Skip to content

Maximum integer part and scale #75

Closed

Description

@nesquikm

I couldn't find any mention of any restrictions, but as far as I can see the integer and fractional parts are represented by the BigInt. However, the simple test below breaks when maxScale>18 or maxInts>18:

import 'package:test/test.dart';
import 'package:money2/money2.dart';

void main() {
  const maxScale = 18;
  const maxInts = 18;

  setUp(() {
    for (var scale = 0; scale <= maxScale; scale++) {
      final c = Currency.create('C$scale', scale, symbol: '=$scale=');
      Currencies().register(c);
    }
  });

  test('scale 0-$maxScale test', () {
    for (var scale = 0; scale <= maxScale; scale++) {
      final c = Currencies().find('C$scale');
      expect(c, isNotNull);
      final str = scale == 0 ? '1' : '1.${'0' * (scale - 1)}1';
      final fmt = scale == 0 ? 'S#' : 'S#.${'#' * scale}';
      expect(Money.parseWithCurrency(str, c!).format(fmt), '=$scale=$str', reason: 'Failed with $scale scale');
    }
  });

  test('integers 0-$maxInts test', () {
    for (var ints = 0; ints <= maxInts; ints++) {
      final c = Currencies().find('C0');
      expect(c, isNotNull);
      final str = ints == 0 ? '0' : '9' * ints;
      final fmt = 'S#';
      expect(Money.parseWithCurrency(str, c!).format(fmt), '=0=$str', reason: 'Failed with $ints ints');
    }
  });

  test('scale 0-$maxScale and integers 0-$maxInts test', () {
    for (var scale = 0; scale <= maxScale; scale++) {
      for (var ints = 0; ints <= maxInts; ints++) {
        final c = Currencies().find('C$scale');
        expect(c, isNotNull);
        final intsStr = ints == 0 ? '0' : '9' * ints;
        final str = scale == 0 ? intsStr : '$intsStr.${'0' * (scale - 1)}1';
        final fmt = scale == 0 ? 'S#' : 'S#.${'#' * scale}';
        expect(Money.parseWithCurrency(str, c!).format(fmt), '=$scale=$str',
            reason: 'Failed with $scale scale, $ints ints');
      }
    }
  });
}

Activity

  1. bsutton commented on Mar 7, 2023

    @bsutton
    Collaborator

    So I'm uncertain what action your are looking to be taken?

  2. nesquikm commented on Mar 7, 2023

    @nesquikm
    Author

    There is no mention of border conditions in the documentation except AmountTooLargeException which is confusing because the library does not work correctly even at smaller scale and without num. First of all, I was looking for an answer to the question at what point does the library stopping working in my test, assuming that I missed something.

  3. nesquikm commented on Mar 7, 2023

    @nesquikm
    Author

    And it is not Fixed: problem, I just wrote similar test.

  4. bsutton commented on Apr 16, 2023

    @bsutton
    Collaborator

    @nesquikm so I ran your unit tests and they all pass?

  5. bsutton commented on Apr 16, 2023

    @bsutton
    Collaborator

    What is your test environment?
    I'm running 64bit linux - ubuntu 22.04
    dart 2.19.5

  6. nesquikm commented on Apr 16, 2023

    @nesquikm
    Author

    As I mention before, I offered Fixed.parse() as a fix for Money.parseWithCurrency() bug. You just added test for Fixed.parse(), so it should pass :)

  7. bsutton commented on Apr 16, 2023

    @bsutton
    Collaborator
  8. nesquikm commented on Apr 17, 2023

    @nesquikm
    Author

    Yeah, there is test for money (also you can find it in the first comment), try to increase maxScale or maxInts to see the problem.

    And this is my temporary fix.

  9. bsutton commented on Apr 29, 2023

    @bsutton
    Collaborator

    do you have a specific requirement?
    Otherwise, I'm inclined to just document the limit.

  10. bsutton commented on Feb 17, 2024

    @bsutton
    Collaborator

    I've documented the limits here:
    https://money2.onepub.dev/

    and I've opened an enhancement request to increase the limits.

  11. nesquikm commented on Feb 14, 2025

    @nesquikm
    Author

    I just found that Money.fromJson and toJson have similar problems (they use toInt()). So I added some tests and updated my package that fixes all of these problems: https://pub.dev/packages/money2_fixer

    For context you can run example:

    Test with: integers: 3, decimals: 18
    String: 999.999999999999999999
    Pattern: 0.################## S
    Parse and format:
      with Money.parse: 999.999999999999999999 SV
      with improved method: 999.999999999999999999 SV
    To and from json:
     parsing, integer part: 999, decimal part: 999999999999999999
      to and from Money.json: integer part: 999  decimal part: 999999999999999999
      to and from json improved: integer part: 999  decimal part: 999999999999999999
    
    Test with: integers: 3, decimals: 19
    String: 999.9999999999999999999
    Pattern: 0.################### S
    Parse and format:
      with Money.parse: -842.8297329635842064385 SV
      with improved method: 999.9999999999999999999 SV
    To and from json:
     parsing, integer part: 999, decimal part: 9999999999999999999
      to and from Money.json: integer part: 999  decimal part: 9223372036854775807
      to and from json improved: integer part: 999  decimal part: 9999999999999999999
    
    Test with: integers: 19, decimals: 3
    String: 9999999999999999999.999
    Pattern: 0.### S
    Parse and format:
      with Money.parse: 9223372036854775807.999 SV
      with improved method: 9999999999999999999.999 SV
    To and from json:
     parsing, integer part: 9999999999999999999, decimal part: 999
      to and from Money.json: integer part: 9223372036854775807  decimal part: 999
      to and from json improved: integer part: 9999999999999999999  decimal part: 999
    
  12. bsutton commented on Feb 14, 2025

    @bsutton
    Collaborator
  13. nesquikm commented on Feb 14, 2025

    @nesquikm
    Author

    You are of course right, I will try to make a PR as soon as I have time.
    According to statistics, many users are already using money2_fixer, so I need to fix it as soon as possible.

  14. bsutton commented on Feb 14, 2025

    @bsutton
    Collaborator

    can I suggest that we merge the two projects and I add you as a contributor to money2 and then mark money2_fixer as deprecated.

    I'm always happy to have help.

  15. nesquikm commented on Feb 15, 2025

    @nesquikm
    Author

    Yes, of course. It would probably be nice to add tests from money2_fixer too.

  16. bsutton commented on Feb 15, 2025

    @bsutton
    Collaborator
  17. nesquikm commented on Feb 18, 2025

    @nesquikm
    Author

    So, I created two PR:

    onepub-dev/fixed#22
    #91

    But I think we should eliminate all the rest toInt()/fromInt(), and check math operations for big int support.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions