Using percent style with a decimal value (e.g. '7.8%') throws 'FormatException" in some locales
Environment
react-native -v: react-native-cli: 2.0.1 react-native: 0.55.4npm ls rnpm-plugin-windows:npm ls react-native-windows:node -v: v7.4.0npm -v: 4.0.5yarn --version: 0.27.5
Then, specify:
- Target Platform: UWP, WPF
- Target Platform Version(s): .NET 4.5.1, .NET 4.6
- Target Device(s): Desktop
- Development Operating System: Windows 10, Windows 7
- Visual Studio Version: 2015, 2017
Steps to Reproduce
- Set Operating System to a Locale that uses a comma in decimal number format
- Use a percent style with a fractional value
const styles = StyleSheet.create({ welcome: { fontSize: 20, textAlign: 'center', margin: '7.8%' } });
- Run the application
Expected Behavior
Application should not cause a FormatException and should apply the percentage value appropriately.
Actual Behavior
When using the percent mechanism in a style where the value is a decimal (e.g. '7.8%') a FormatException will be thrown if the application is running in a locale/culture where the culture's decimal format is defined as
The FormatException happens in LayoutShadowNode.cs in the method:
YogaValue ToYogaValue(JValue value).
in the code:
if (s.EndsWith("%")) { return YogaValue.Percent(float.Parse(s.Substring(0, s.Length - 1))); }
The float.Parse method is called without specifying an IFormatProvider, which will cause .NET to use the current locale (e.g. France, Germany, Finland) for the parsing. Those versions of the float.Parse method expect a comma instead of a period. Since the declarative language uses a period for this value a FormatException will occur.
The solution is to declare that an invariant culture conversion to be used for the parsing...
return YogaValue.Percent(float.Parse(s.Substring(0, s.Length - 1), CultureInfo.InvariantCulture));
Reproducible Demo
ReactNative Example
C# Example
@pre10der89 closing since your PR was merged.