@@ -17,6 +17,12 @@ export class StringToDecimalModifier extends BaseModifier<StringToDecimalProps,
1717
1818 private static emptyFormattedValue : string ;
1919
20+ public componentWillMount ( ) : void {
21+ if ( this . props . delimiter == null || this . props . delimiter . length !== 1 ) {
22+ throw new Error ( `StringToDecimalModifier: delimiter prop has to be exactly one character long.` ) ;
23+ }
24+ }
25+
2026 public Format ( value : FieldValue ) : FieldValue {
2127 if ( value !== EMPTY_VALUE ) {
2228 return value . toString ( ) ;
@@ -52,7 +58,13 @@ export class StringToDecimalModifier extends BaseModifier<StringToDecimalProps,
5258 const delimiter = this . props . delimiter ! ;
5359
5460 const leadingMinus = negative ? "-" : "" ;
55- const regex = new RegExp ( `[^0-9\\${ delimiter } ]+` , "g" ) ;
61+
62+ // Include delimiter in regex if precision is non-zero
63+ const regexPattern = this . props . precision === 0 ?
64+ `[^0-9]+` :
65+ `[^0-9\\${ delimiter } ]+` ;
66+
67+ const regex = new RegExp ( regexPattern , "g" ) ;
5668 const extractedValue : string = this . LeaveOnlyFirstDelimiter (
5769 value . replace ( regex , "" ) ,
5870 delimiter ) ;
@@ -78,7 +90,8 @@ export class StringToDecimalModifier extends BaseModifier<StringToDecimalProps,
7890 }
7991
8092 transitionalValue = leadingMinus + transitionalValue ;
81- const numValue = Number ( transitionalValue ) ;
93+ const delimiterFixed = transitionalValue . replace ( delimiter , "." ) ;
94+ const numValue = Number ( delimiterFixed ) ;
8295
8396 return {
8497 Value : numValue ,
0 commit comments