Numerus 是一个 Dart 包,提供将整数转换为罗马数字以及反之的扩展功能。它通过在整数和字符串类型上直接提供直观的方法,简化了对罗马数字表示形式的操作。非常适合用于教育类应用、历史数据处理或任何需要罗马数字格式化的应用程序。该包支持所有主要平台,包括 Android、iOS、Web、macOS、Windows 和 Linux,确保广泛的兼容性。使用简洁、精简的语法,可提高代码可读性,并减少处理数字转换时的样板代码。
适合场景
将日期转换为罗马数字
数制教育应用
历史文档数字化
以罗马数字显示章号
使用罗马数字进行游戏得分跟踪
主要特点
简单的整数转罗马数字转换
字符串转整数解析
Dart 扩展方法
跨平台支持
最小 API 表面
强大的错误处理
适合对象
开发数学工具的开发者
教育软件
具有历史主题的应用程序
需要可读数字格式的项目
偏好简洁语法的用户
使用建议
仅处理标准罗马数字(超出常见规则的减法变体除外)
由于罗马数字的限制,不支持非常大的数字(>3999)
不支持非拉丁文字的本地化
需要手动验证边缘情况,例如无效的输入字符串
原始 README
以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。
Numerus
Dart extensions for translating decimal numbers to Roman numerals and vice versa.
Negative numbers are not supported.
Functions will return null in place of generating nonsense.
Zero can be specified as an option to the RomanNumeralConfig.
"Common" Roman numeral format used in modern times (i.e. MDCLXVI) is
the default RomanNumeralConfig style. There are other styles: vinculum, apostrophus,
and a compact apostrophus that uses single Unicode characters instead
of tally marks. See the example below.
Examples
import'package:numerus/numerus.dart';
voidmain() {
/// 1) Nice and easy, default config is the "common" config.var n = 418;
print(n.toRomanNumeralString());
// 'CDXVIII'var str = 'CDXVIII';
print(str.isValidRomanNumeralValue());
// trueprint(str.toRomanNumeralValue());
// 418/// 2) You can specify other [RomanNumeralConfig]'s to change the style./// You can set the config globally, if you'd like.RomanNumerals.romanNumeralsConfig = VinculumRomanNumeralsConfig();
/// 3) Vinculum config style
n = 3449671;
print(n.toRomanNumeralString());
// M̅M̅M̅C̅D̅X̅L̅MX̅DCLXXI
str = 'M̅M̅M̅C̅D̅X̅L̅MX̅DCLXXI';
print(str.isValidRomanNumeralValue());
// trueprint(str.toRomanNumeralValue());
// 3449671/// 4) You can specify the config inline, too./// 5) Apostrophus style
n = 2449671;
print(n.toRomanNumeralString(config: ApostrophusRomanNumeralsConfig()));
// CCCCIↃↃↃↃCCCCIↃↃↃↃCCCIↃↃↃIↃↃↃↃCCIↃↃIↃↃↃCIↃCCIↃↃIↃCLXXI/// 6) ... but frankly, globally set is probably the most common use caseRomanNumerals.romanNumeralsConfig = ApostrophusRomanNumeralsConfig();
str = 'CCCCIↃↃↃↃCCCCIↃↃↃↃCCCIↃↃↃIↃↃↃↃCCIↃↃIↃↃↃCIↃCCIↃↃIↃCLXXI';
print(str.isValidRomanNumeralValue());
// trueprint(str.toRomanNumeralValue());
// 2449671/// 7) The compact Apostrophus style uses special Unicode characters/// just for these symbols.RomanNumerals.romanNumeralsConfig = CompactApostrophusRomanNumeralsConfig();
n = 347449;
print(n.toRomanNumeralString());
// ↈↈↈↂↇↁↀↀCCCCXLIX// NOTE: if you see missing characters in this example, then you'll need to// install a font that supports them.
str = 'ↈↈↈↂↇↁↀↀCCCCXLIX';
print(str.isValidRomanNumeralValue());
// trueprint(str.toRomanNumeralValue());
// 347449/// 8) You can specify a "nulla" (zero placeholder) if you want/// to support zero easily.RomanNumerals.romanNumeralsConfig = CommonRomanNumeralsConfig(nulla: 'N');
n = 0;
print(n.toRomanNumeralString());
// N
str = 'N';
print(str.isValidRomanNumeralValue());
// trueprint(str.toRomanNumeralValue());
// 0
}
Features and bugs
Please file feature requests and bugs at the issue tracker.
Local development
Set up an environment for local development
Install Dart or Flutter
Install the coverage tool
$ dart pub global activate coverage
Install lcov on your system.
e.g. macOS with brew:
$ brew install lcov
Generate code coverage from tests
$ ./bin/coverage-gen
# If you are on macOS and would like the report to immediately# open, then run the following (not suitable for automation):
$ ./bin/coverage-gen -i