Home > Software design >  How to increment number in roman word in flutter/dart
How to increment number in roman word in flutter/dart

Time:05-10

I need to increment number dynamically in Roman

something like:

CodePudding user response:

You can increment by int and convert it to number in roman by this library :

numerus

final n = 2;
print(n.toRomanNumeralString());

CodePudding user response:

You could write it yourself as an exercise, or you can use a package like numerus to do this job for you.

CodePudding user response:

From how do I make an integer to roman algorithm in dart?, you can get the following:

const List<int> arabianRomanNumbers = [
  1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1
];

final builder = StringBuffer();
  for (var a = 0; a < arabianRomanNumbers.length; a  ) {
    final times = (num / arabianRomanNumbers[a]).truncate(); // equals 1 only when arabianRomanNumbers[a] = num
    // executes n times where n is the number of times you have to add
    // the current roman number value to reach current num.
    builder.write(romanNumbers[a] * times);
    num -= times * arabianRomanNumbers[a]; // subtract previous roman number value from num
  }

 return builder.toString();
  • Related