parse method
Parses input and returns the numeric value.
Throws FormatException when input cannot be parsed by this codec.
Implementation
@override
num parse(String input) {
var text = input.trim();
if (text.isEmpty) {
throw FormatException('Expected an RMB uppercase amount.', input);
}
final negative = text.startsWith('负');
if (negative) {
text = text.substring(1);
if (text.isEmpty) {
throw FormatException('Expected an RMB uppercase amount.', input);
}
}
if (prefix.isNotEmpty && text.startsWith(prefix)) {
text = text.substring(prefix.length);
}
if (text.endsWith(wholeSuffix)) {
text = text.substring(0, text.length - wholeSuffix.length);
} else if (text.endsWith('正')) {
text = text.substring(0, text.length - 1);
}
if (text.isEmpty) {
throw FormatException('Expected an RMB uppercase amount.', input);
}
var yuan = 0;
var lower = text;
final yuanIndex = text.indexOf('元');
if (yuanIndex >= 0) {
final yuanText = text.substring(0, yuanIndex);
if (yuanText.isEmpty) {
throw FormatException('Expected an RMB amount yuan value.', input);
}
yuan = _financial.parse(yuanText);
lower = text.substring(yuanIndex + 1);
}
if (lower.contains('角') || lower.contains('分')) {
lower = lower.replaceFirst(_leadingZeroPattern, '');
} else if (lower.trim().isNotEmpty) {
throw FormatException('Unexpected RMB amount token.', input);
}
var jiao = 0;
var fen = 0;
final jiaoIndex = lower.indexOf('角');
if (jiaoIndex >= 0) {
final jiaoText = lower.substring(0, jiaoIndex);
jiao = _parseSingleDigit(jiaoText, input);
lower = lower.substring(jiaoIndex + 1);
}
final fenIndex = lower.indexOf('分');
if (fenIndex >= 0) {
final fenText = lower.substring(0, fenIndex);
fen = _parseSingleDigit(fenText, input);
lower = lower.substring(fenIndex + 1);
}
if (lower.trim().isNotEmpty) {
throw FormatException('Unexpected RMB amount token.', input);
}
final cents = yuan * 100 + jiao * 10 + fen;
final value = cents % 100 == 0 ? cents ~/ 100 : cents / 100;
return negative ? -value : value;
}