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