Skip to content

Commit

Permalink
fixes #61
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyryan committed Sep 30, 2021
1 parent dffba03 commit 8b5298a
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## [2.4.0]
Fixed https://github.com/caseyryan/flutter_multi_formatter/issues/61
Fixed orphan leading period formatting in strings like
$.5. Now they are formatted correctly to $0.5, not $500.00
## [2.3.8]
One more small fix
## [2.3.7]
Expand Down
2 changes: 1 addition & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ class _MyHomePageState extends State<MyHomePage> {
@override
void initState() {
super.initState();

PhoneInputFormatter.addAlternativePhoneMasks(
countryCode: 'NZ',
alternativeMasks: [
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ packages:
path: ".."
relative: true
source: path
version: "2.3.8"
version: "2.4.0"
flutter_test:
dependency: "direct dev"
description: flutter
Expand Down
35 changes: 19 additions & 16 deletions lib/formatters/formatter_extension_methods.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ THE SOFTWARE.
*/

import 'money_input_enums.dart';
import 'money_input_formatter.dart' as money;
import 'money_input_formatter.dart' as moneyFormatter;

/// WARNING! This stuff requires Dart SDK version 2.6+
/// so if your code is supposed to be running on
Expand All @@ -52,20 +52,23 @@ extension NumericInputFormatting on num {
/// end of your resulting string like 1,250€ instead of €1,250
/// [useSymbolPadding] adds a space between the number and trailing / leading symbols
/// like 1,250€ -> 1,250 € or €1,250€ -> € 1,250
String toCurrencyString(
{int mantissaLength = 2,
ThousandSeparator thousandSeparator = ThousandSeparator.Comma,
ShorteningPolicy shorteningPolicy = ShorteningPolicy.NoShortening,
String leadingSymbol = '',
String trailingSymbol = '',
bool useSymbolPadding = false}) {
return money.toCurrencyString(this.toString(),
mantissaLength: mantissaLength,
leadingSymbol: leadingSymbol,
shorteningPolicy: shorteningPolicy,
thousandSeparator: thousandSeparator,
trailingSymbol: trailingSymbol,
useSymbolPadding: useSymbolPadding);
String toCurrencyString({
int mantissaLength = 2,
ThousandSeparator thousandSeparator = ThousandSeparator.Comma,
ShorteningPolicy shorteningPolicy = ShorteningPolicy.NoShortening,
String leadingSymbol = '',
String trailingSymbol = '',
bool useSymbolPadding = false,
}) {
return moneyFormatter.toCurrencyString(
this.toString(),
mantissaLength: mantissaLength,
leadingSymbol: leadingSymbol,
shorteningPolicy: shorteningPolicy,
thousandSeparator: thousandSeparator,
trailingSymbol: trailingSymbol,
useSymbolPadding: useSymbolPadding,
);
}
}

Expand Down Expand Up @@ -94,7 +97,7 @@ extension StringInputFormatting on String {
String leadingSymbol = '',
String trailingSymbol = '',
bool useSymbolPadding = false}) {
return money.toCurrencyString(this.toString(),
return moneyFormatter.toCurrencyString(this.toString(),
mantissaLength: mantissaLength,
leadingSymbol: leadingSymbol,
shorteningPolicy: shorteningPolicy,
Expand Down
2 changes: 1 addition & 1 deletion lib/formatters/formatter_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ String toNumericString(
bool allowHyphen = true,
}) {
if (inputString == null) return '';

var startsWithPeriod = numericStringStartsWithOrphanPeriod(
inputString,
);

var regexWithoutPeriod = allowHyphen ? _digitRegExp : _positiveDigitRegExp;
var regExp = allowPeriod ? _digitWithPeriodRegExp : regexWithoutPeriod;
var result = inputString.splitMapJoin(
Expand Down
14 changes: 12 additions & 2 deletions lib/formatters/money_input_formatter.dart
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,9 @@ class MoneyInputFormatter extends TextInputFormatter {
selection: selection,
text: preparedText,
);
} /// stop isErasing
}

/// stop isErasing
bool oldStartsWithLeading = leadingSymbol.isNotEmpty &&
oldValue.text.startsWith(
Expand All @@ -331,7 +333,7 @@ class MoneyInputFormatter extends TextInputFormatter {
);

/// This check is necessary because if an input looks like this
/// $.5, toCurrencyString() method will convert it to
/// $.5, toCurrencyString() method will convert it to
/// $0.5 and the selection must also be shifted by 1 symbol to the right
var startsWithOrphanPeriod = numericStringStartsWithOrphanPeriod(newText);
var formattedValue = toCurrencyString(
Expand Down Expand Up @@ -533,6 +535,10 @@ String toCurrencyString(
bool useSymbolPadding = false,
}) {
var swapCommasAndPreriods = false;
if (mantissaLength <= 0) {
mantissaLength = 0;
}

String? tSeparator;
switch (thousandSeparator) {
case ThousandSeparator.Comma:
Expand All @@ -555,6 +561,10 @@ String toCurrencyString(
}
// print(thousandSeparator);
value = value.replaceAll(_repeatingDots, '.');
if (mantissaLength == 0) {
var substringEnd = value.lastIndexOf('.');
value = value.substring(0, substringEnd);
}
value = toNumericString(value, allowPeriod: mantissaLength > 0);
var isNegative = value.contains('-');

Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: flutter_multi_formatter
description: A package of formatters for international phone numbers, credit / debit cards and a masked formatter
version: 2.3.8
version: 2.4.0
homepage: https://github.com/caseyryan/flutter_multi_formatter

environment:
Expand Down

0 comments on commit 8b5298a

Please sign in to comment.