numeric_utils library

A comprehensive library for working with numeric types in Dart.

This library provides extensions and utilities for precise numeric operations, particularly useful for financial calculations, measurements, and scenarios requiring exact decimal arithmetic.

Key Features

  • Rounding: Multiple rounding modes (floor, ceil, halfUp, halfEven, etc.)
  • Formatting: Localized currency, percentage, and decimal formatting
  • Parsing: Parse fractions, mixed numbers, and various numeric formats
  • Validation: Range checking, tolerance comparisons, multiple checking
  • Constants: Common fractions, percentages, and numeric values
  • Percentage Calculations: Semantic helpers for percentage operations

Quick Start

import 'package:numeric_utils/numeric_utils.dart';
import 'package:rational/rational.dart';

void main() {
  // Exact decimal arithmetic
  final price = Rational.parse('19.99');
  final taxRate = Rational.parse('0.08');
  final total = price * (Rational.one + taxRate);

  // Formatted output
  print(total.toCurrencyString(locale: 'en_US')); // $21.59

  // Percentage calculations
  final discount = Rational.parse('15'); // 15%
  final discountedPrice = discount.percentChangeOn(price);
  print(discountedPrice.toCurrencyString(locale: 'en_US')); // $22.99

  // Common rounding
  final value = Rational.parse('1.234');
  print(value.toNearestDecimal(2)); // 617/500 (1.234 rounded to 2 places)
  print(value.toDecimalString(2)); // "1.23" (formatted string)
}

Importing

Import the entire library:

import 'package:numeric_utils/numeric_utils.dart';

Or import specific parts:

import 'package:numeric_utils/extensions/numeric_extensions.dart';
import 'package:numeric_utils/constants/numeric_constants.dart';

Classes

BigIntConstants
Provides useful constants for the BigInt type
IntConstants
Provides useful constants for the int type
RationalConstants
Provides useful constants for the Rational type
RationalParsing
A utility class for parsing strings into Rational objects with round-trip support. Unlike Rational.parse, which only handles integers, decimals, and scientific notation, this class can parse fractions (e.g., "7/4") produced by Rational.toString, ensuring compatibility with its output format as well as mixed fractions (e.g., "1 3/4").

Enums

RoundingMode
Defines standard rounding modes for numerical rounding

Extensions

BigIntIsInRangeExtension on BigInt
Extension on BigInt to support isInRange
BigIntMultipleOfExtension on BigInt
Extension on BigInt to support isMultipleOf, isInRange
BigIntRoundedDivisionExtension on BigInt
Extension on BigInt to support division with rounding
BigIntToleranceExtension on BigInt
Extension on BigInt to support isWithinTolerance
DoubleIsInRangeExtension on double
Extension on double to support isInRange
DoubleMultipleOfExtension on double
Extension on double to support isMultipleOf
DoubleToleranceExtension on double
Extension on double to support isWithinTolerance and isCloseTo
IntIsInRangeExtension on int
Extension on int to support isInRange
IntMultipleOfExtension on int
Extension on int to support isMultipleOf
IntToleranceExtension on int
Extension on int to support isWithinTolerance
RationalCommonRoundingExtension on Rational
An extension on Rational to provide some common rounding functionality
RationalFormattingExtension on Rational
An extension on Rational to provide additional formatting functionality
RationalIsInRangeExtension on Rational
Extension on Rational to support isInRange
RationalMultipleOfExtension on Rational
Extension on Rational to support isMultipleOf
RationalPercentageExtension on Rational
Extension on Rational to provide percentage calculation utilities
RationalRoundingExtension on Rational
An extension on Rational to provide rounding functionality
RationalToleranceExtension on Rational
Extension on Rational to support isWithinTolerance and isCloseTo