Skip to main content

simple_portable_numeric

(en)Japanese ver is here.
(ja) この解説の日本語版はここ にあります。

Overview

simple_portable_numeric is an arbitrary-precision decimal and rational arithmetic library that gives identical results in Dart and Python.

Both versions are built only on arbitrary-precision integers (Dart BigInt, Python int) and follow one shared specification. Platform libraries such as Python's decimal (28 significant digits by default) are not used, so the number of digits, rounding and string format never differ between the languages.

Features

  • PortableDecimal: exact + - * with no digit limit. Division always takes an explicit scale and rounding mode.
  • PortableRational: exact + - * /, always kept reduced.
  • Six rounding modes: halfEven, halfUp, down, floor, ceiling, up (away from zero).
  • Strict and stable string format, suitable for storing values as text (e.g. in a database).
  • Equality by value (1.50 == 1.5), while the string keeps the scale ("1.50").
  • Correctly rounded conversion to double, identical in both languages.
  • No dependencies.
  • Dart version available.
    → https://pub.dev/packages/simple_portable_numeric

Quick Start

from simple_portable_numeric import EnumRoundingMode, PortableDecimal, PortableRational

# Decimal
a = PortableDecimal.parse("1.50")
b = PortableDecimal.parse("2.3")
print(a + b)  # 3.80
print(a * b)  # 3.450
print(a == PortableDecimal.parse("1.5"))  # True
print(PortableDecimal.parse("1").divide(
    PortableDecimal.parse("3"), 5, EnumRoundingMode.halfEven))  # 0.33333
print(PortableDecimal.parse("2.5").set_scale(0, EnumRoundingMode.halfEven))  # 2

# Rational
r = PortableRational.parse("1/3") + PortableRational.parse("1/6")
print(r)  # 1/2
print(r.to_decimal(2, EnumRoundingMode.halfUp))  # 0.50
print(PortableRational.parse("0.75"))  # 3/4

# Conversion
print(PortableDecimal.parse("0.1").to_rational())  # 1/10
print(PortableRational.parse("1/3").to_float())  # 0.3333333333333333

# Integers and powers
print(PortableDecimal.parse("2.5").to_int(EnumRoundingMode.up))  # 3
print(PortableDecimal.parse("1.00").is_integer())  # True
print(PortableDecimal.parse("1.5") ** 2)  # 2.25
print(PortableRational.parse("2/3") ** -2)  # 9/4

# Exact decimal form and trailing zeros
print(PortableRational.parse("3/8").to_decimal_exact())  # 0.375
print(PortableDecimal.parse("1.500").strip_trailing_zeros())  # 1.5

# Lenient parse (e.g. values exported from spreadsheet software)
print(PortableDecimal.parse_lenient("1.2E-05"))  # 0.000012

Notes

  • parse accepts only plain notation such as "-12.340" or "3/4", so that text stored in a database has one canonical form. Exponent notation ("1e3"), whitespace and non-ASCII digits are rejected.
  • PortableDecimal.parse_lenient additionally accepts exponent notation such as "1.2E-05" (absolute exponent up to 324) and ".5" / "1.". It is for reading external data such as values exported from spreadsheet software. Whitespace, "1,000", "12%", NaN and Infinity are still rejected.
  • PortableDecimal and PortableRational are never mixed implicitly. Convert explicitly with to_rational() / to_decimal(scale, rounding).
  • Conversion from double is not provided directly. PortableDecimal.parse_lenient(repr(x)) gives a value that converts back to the same double, but the scale may differ between languages, because the string forms of doubles differ (e.g. 1e16).
  • Hash values are consistent with equality, but are not the same across languages.

Support

This package is developed and maintained by me personally as an open-source project. For bug reports and feature requests, please use GitHub Issues.

If you need paid support, consulting, or custom development (e.g. priority support, design advice, or implementation help), please contact my company:

SimpleAppli Inc. https://simpleappli.com/en/index_en.html

About version control

The C part will be changed at the time of version upgrade. However, versions less than 1.0.0 may change the calculation results or the string format regardless of the following rules.

  • Specification changes that change the calculation results or the string format.
    • C.X.X
  • Adding methods, etc.
    • X.C.X
  • Minor changes and bug fixes.
    • X.X.C

License

This software is released under the Apache-2.0 License, see LICENSE file.

Copyright 2026 Masahide Mori

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Trademarks

  • “Dart” and “Flutter” are trademarks of Google LLC. This package is not developed or endorsed by Google LLC.

  • “Python” is a trademark of the Python Software Foundation. This package is not affiliated with the Python Software Foundation.

  • GitHub and the GitHub logo are trademarks of GitHub, Inc. This package is not affiliated with GitHub, Inc.

Metadata

Release files for simple-portable-numeric 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for simple-portable-numeric 0.1.0
File Size Uploaded Provenance Actions
simple_portable_numeric-0.1.0.tar.gz 19.0 kB Without provenance Details for simple_portable_numeric-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for simple-portable-numeric 0.1.0
File Interpreter ABI Platform Actions
simple_portable_numeric-0.1.0-py3-none-any.whl Python 3 none any Details for simple_portable_numeric-0.1.0-py3-none-any.whl

Total release size: 37.3 kB

Release files / simple_portable_numeric-0.1.0.tar.gz

Download URL simple_portable_numeric-0.1.0.tar.gz
Size 19.0 kB
Tags Source
SHA-256 checksum
How to use checksums
7da53d6535e19bca140381c7f21c973154fe3517fecca4cba024ac8118d2bec8
BLAKE2b-256 checksum
How to use checksums
23489c30766f5fac23a6f340ecba42e4b94b92afec7a8461bdcfdbabf709dcfe
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Release files / simple_portable_numeric-0.1.0-py3-none-any.whl

Download URL simple_portable_numeric-0.1.0-py3-none-any.whl
Size 18.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
bd3e17da9aa6c23720e4f5136beaa7a5861a356558a4e13c7eadff49a5197977
BLAKE2b-256 checksum
How to use checksums
e915e60d4070e9167084c84fe828bf2a1eb1adf90686178cd7ed82515cdfeaec
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.3

Security for simple-portable-numeric 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page