Skip to content

Commit 1714c05

Browse files
committed
culture info taken into account when parsing decimal | #10
1 parent 124b9ca commit 1714c05

File tree

8 files changed

+35
-57
lines changed

8 files changed

+35
-57
lines changed

NumericWordsConversion.sln

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29509.3
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.8.34330.188
55
MinimumVisualStudioVersion = 10.0.40219.1
6-
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumericWordsConversion", "NumericWordsConversion\NumericWordsConversion.csproj", "{85861DC0-DE30-4D37-9C07-FAFE66409A47}"
7-
EndProject
86
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTests", "UnitTests\UnitTests.csproj", "{06BB2AD6-C600-4221-8E21-2EBDDFECF31B}"
97
EndProject
108
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleTest", "ConsoleTest\ConsoleTest.csproj", "{1CD938FE-2CE9-483F-A4BB-9E099BA7D76F}"
119
EndProject
10+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumericWordsConversion", "NumericWordsConversion\NumericWordsConversion.csproj", "{457F6EE6-B519-4B02-A17A-B58AE34D8CD2}"
11+
EndProject
1212
Global
1313
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1414
Debug|Any CPU = Debug|Any CPU
1515
Release|Any CPU = Release|Any CPU
1616
EndGlobalSection
1717
GlobalSection(ProjectConfigurationPlatforms) = postSolution
18-
{85861DC0-DE30-4D37-9C07-FAFE66409A47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
19-
{85861DC0-DE30-4D37-9C07-FAFE66409A47}.Debug|Any CPU.Build.0 = Debug|Any CPU
20-
{85861DC0-DE30-4D37-9C07-FAFE66409A47}.Release|Any CPU.ActiveCfg = Release|Any CPU
21-
{85861DC0-DE30-4D37-9C07-FAFE66409A47}.Release|Any CPU.Build.0 = Release|Any CPU
2218
{06BB2AD6-C600-4221-8E21-2EBDDFECF31B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2319
{06BB2AD6-C600-4221-8E21-2EBDDFECF31B}.Debug|Any CPU.Build.0 = Debug|Any CPU
2420
{06BB2AD6-C600-4221-8E21-2EBDDFECF31B}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -27,6 +23,10 @@ Global
2723
{1CD938FE-2CE9-483F-A4BB-9E099BA7D76F}.Debug|Any CPU.Build.0 = Debug|Any CPU
2824
{1CD938FE-2CE9-483F-A4BB-9E099BA7D76F}.Release|Any CPU.ActiveCfg = Release|Any CPU
2925
{1CD938FE-2CE9-483F-A4BB-9E099BA7D76F}.Release|Any CPU.Build.0 = Release|Any CPU
26+
{457F6EE6-B519-4B02-A17A-B58AE34D8CD2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
27+
{457F6EE6-B519-4B02-A17A-B58AE34D8CD2}.Debug|Any CPU.Build.0 = Debug|Any CPU
28+
{457F6EE6-B519-4B02-A17A-B58AE34D8CD2}.Release|Any CPU.ActiveCfg = Release|Any CPU
29+
{457F6EE6-B519-4B02-A17A-B58AE34D8CD2}.Release|Any CPU.Build.0 = Release|Any CPU
3030
EndGlobalSection
3131
GlobalSection(SolutionProperties) = preSolution
3232
HideSolutionNode = FALSE

NumericWordsConversion/ConversionFactory.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ internal string ConvertDigits(string digits)
8383
if (_options.Culture == Culture.International)
8484
scaleMapIndex = (int)Math.Ceiling((decimal)digits.Length / 3);
8585
else
86-
scaleMapIndex = (digits.Length - 3) < 1 ? 1 : digits.Length / 2;
86+
scaleMapIndex = digits.Length - 3 < 1 ? 1 : digits.Length / 2;
8787
for (int i = scaleMapIndex; i > 0; i--)
8888
{
8989
string inWords;
@@ -97,14 +97,14 @@ internal string ConvertDigits(string digits)
9797
default: //For Everything Greater than hundreds
9898
if (_options.Culture == Culture.International)
9999
{
100-
int length = (digits.Length % ((i - 1) * 3 + 1)) + 1;
100+
int length = digits.Length % ((i - 1) * 3 + 1) + 1;
101101
string hundreds = digits.Substring(0, length);
102102
digits = digits.Remove(0, length);
103103
inWords = ToHundredthWords(hundreds);
104104
}
105105
else
106106
{
107-
int length = (digits.Length % 2 == 0) ? 1 : 2;
107+
int length = digits.Length % 2 == 0 ? 1 : 2;
108108
string hundreds = digits.Substring(0, length);
109109
digits = digits.Remove(0, length);
110110
inWords = ToTensWord(hundreds);

NumericWordsConversion/Culture.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,17 +5,17 @@
55
/// </summary>
66
public enum Culture
77
{
8-
/// <summary>
9-
/// Uses the International Numeral System for Words Conversion
10-
/// </summary>
8+
/// <summary>
9+
/// Uses the International Numeral System for Words Conversion
10+
/// </summary>
1111
International,
12-
/// <summary>
13-
/// Uses the Nepalese Numeral System for Words Conversion
14-
/// </summary>
12+
/// <summary>
13+
/// Uses the Nepalese Numeral System for Words Conversion
14+
/// </summary>
1515
Nepali,
16-
/// <summary>
17-
/// Uses the Hindi Numeral System for Words Conversion
18-
/// </summary>
16+
/// <summary>
17+
/// Uses the Hindi Numeral System for Words Conversion
18+
/// </summary>
1919
Hindi
2020
}
2121
}

NumericWordsConversion/CurrencyWordsConverter.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class CurrencyWordsConverter
1616
/// </summary>
1717
public CurrencyWordsConverter()
1818
{
19-
this._options = GlobalOptions.CurrencyWordsOptions;
19+
_options = GlobalOptions.CurrencyWordsOptions;
2020
_conversionFactory = Utilities.InitializeConversionFactory(_options);
2121
}
2222

@@ -25,7 +25,7 @@ public CurrencyWordsConverter()
2525
/// </summary>
2626
public CurrencyWordsConverter(CurrencyWordsConversionOptions options)
2727
{
28-
this._options = options;
28+
_options = options;
2929
_conversionFactory = Utilities.InitializeConversionFactory(_options);
3030
}
3131
#endregion
@@ -47,18 +47,18 @@ public string ToWords(decimal number)
4747
CultureInfo.InvariantCulture)
4848
.Split('.')
4949
.ElementAtOrDefault(1) ?? Empty;
50-
if (decimal.Parse(integralDigitsString) <= 0 && decimal.Parse(fractionalDigitsString) <= 0) return Empty;
50+
if (decimal.Parse(integralDigitsString, CultureInfo.InvariantCulture) <= 0 && decimal.Parse(fractionalDigitsString, CultureInfo.InvariantCulture) <= 0) return Empty;
5151

5252
string integralWords = Empty;
53-
if (decimal.Parse(integralDigitsString) > 0)
53+
if (decimal.Parse(integralDigitsString, CultureInfo.InvariantCulture) > 0)
5454
{
5555
integralWords = _conversionFactory.ConvertDigits(integralDigitsString);
5656
integralWords = _options.CurrencyNotationType == NotationType.Prefix
5757
? _options.CurrencyUnit + " " + integralWords
5858
: integralWords + " " + _options.CurrencyUnit;
5959
}
6060

61-
if (int.Parse(fractionalDigitsString) <= 0 || IsNullOrEmpty(fractionalDigitsString)) return Concat(integralWords, (IsNullOrEmpty(_options.EndOfWordsMarker) ? "" : " " + _options.EndOfWordsMarker)).CapitalizeFirstLetter();
61+
if (int.Parse(fractionalDigitsString) <= 0 || IsNullOrEmpty(fractionalDigitsString)) return Concat(integralWords, IsNullOrEmpty(_options.EndOfWordsMarker) ? "" : " " + _options.EndOfWordsMarker).CapitalizeFirstLetter();
6262

6363
string fractionalWords = _conversionFactory.ConvertDigits(fractionalDigitsString);
6464
fractionalWords = _options.SubCurrencyNotationType == NotationType.Prefix

NumericWordsConversion/NumericWordsConfiguration.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static class GlobalOptions
3434
public static NumericWordsConverter NumericWordsConverter { get; internal set; } = new NumericWordsConverter(NumericWordsOptions);
3535

3636
public static CurrencyWordsConverter CurrencyWordsConverter { get; internal set; } = new CurrencyWordsConverter(CurrencyWordsOptions);
37-
}
37+
}
3838

3939
public class OptionsInitializer
4040
{

NumericWordsConversion/NumericWordsConversion.csproj

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,14 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFramework>netcoreapp3.0</TargetFramework>
5-
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
6-
<Authors>Samyeak Maharjan</Authors>
7-
<Description>Numeric Words Conversion is a C# library for converting numeric into words. The goal is to create a simple customizable library which can easily be used to convert any numbers or currencies to words. Supports Nepali, Hindi and International Numeral System out of the box.</Description>
8-
<Copyright>Samyeak Maharjan</Copyright>
9-
<NeutralLanguage>en</NeutralLanguage>
10-
<Version>2.0.0.0</Version>
11-
<PackageLicenseFile>LICENSE</PackageLicenseFile>
12-
<PackageProjectUrl>https://github.com/Samyeak/NumericWordsConversion.git</PackageProjectUrl>
13-
<RepositoryUrl>https://github.com/Samyeak/NumericWordsConversion.git</RepositoryUrl>
14-
<RepositoryType>github</RepositoryType>
15-
<PackageTags>NumberToText;Numeric; NumericToWords; CurrencyToWords; InWords; Currency In Words; Number In Words; ToWords; AmountToWords; Nepali; Devnagari; Unicode; DecimalToWords; Nepali Number To Words; Amount To Words; Hindi Number To Words; Hindi Currency To Text</PackageTags>
16-
<PackageReleaseNotes>* Added Application Level Configuration
17-
* Added Hindi Numeral System</PackageReleaseNotes>
18-
<SignAssembly>false</SignAssembly>
19-
<DelaySign>false</DelaySign>
20-
<AssemblyOriginatorKeyFile>NumericWordsConversionSignatureKey.snk</AssemblyOriginatorKeyFile>
21-
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
22-
<PackageIconUrl>https://repository-images.githubusercontent.com/233162696/d06bb100-4545-11ea-8853-f816e055791a</PackageIconUrl>
23-
</PropertyGroup>
24-
25-
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
26-
<Optimize>true</Optimize>
4+
<TargetFramework>netstandard2.1</TargetFramework>
5+
<Nullable>enable</Nullable>
276
</PropertyGroup>
287

298
<ItemGroup>
30-
<None Include="..\LICENSE">
31-
<Pack>True</Pack>
9+
<None Include="..\LICENSE" Link="LICENSE">
3210
<PackagePath></PackagePath>
11+
<Pack>True</Pack>
3312
</None>
3413
</ItemGroup>
3514

NumericWordsConversion/NumericWordsConversionOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,6 @@ public string DecimalSeparator
5656
/// In order to use generic algorithm for all the numeral system,
5757
/// this is used to map suitable resources as per different numeral system
5858
/// </summary>
59-
internal int ResourceLimitIndex => this.OutputFormat == OutputFormat.English ? 20 : 100;
59+
internal int ResourceLimitIndex => OutputFormat == OutputFormat.English ? 20 : 100;
6060
}
6161
}

NumericWordsConversion/NumericWordsConverter.cs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public class NumericWordsConverter
2222
/// </summary>
2323
public NumericWordsConverter()
2424
{
25-
this._options = GlobalOptions.NumericWordsOptions;
25+
_options = GlobalOptions.NumericWordsOptions;
2626
_conversionFactory = Utilities.InitializeConversionFactory(_options);
2727
}
2828

@@ -31,7 +31,7 @@ public NumericWordsConverter()
3131
/// </summary>
3232
public NumericWordsConverter(NumericWordsConversionOptions options)
3333
{
34-
this._options = options;
34+
_options = options;
3535
_conversionFactory = Utilities.InitializeConversionFactory(_options);
3636
}
3737
#endregion
@@ -45,13 +45,12 @@ public string ToWords(decimal number)
4545
{
4646
string integralDigitsString = number
4747
.ToString(CultureInfo.InvariantCulture)
48-
.Split('.')
49-
.ElementAt(0);
48+
.Split('.')[0];
5049

5150
decimal fractionalDigits = number % 1;
5251

5352
string integralWords = _conversionFactory.ConvertDigits(integralDigitsString);
54-
string fractionalDigitsString = (_options.DecimalPlaces > -1 ? decimal.Parse(fractionalDigits.ToString($"F{_options.DecimalPlaces}", CultureInfo.InvariantCulture))
53+
string fractionalDigitsString = (_options.DecimalPlaces > -1 ? decimal.Parse(fractionalDigits.ToString($"F{_options.DecimalPlaces}", CultureInfo.InvariantCulture), CultureInfo.InvariantCulture)
5554
.ToString($"G{_options.DecimalPlaces}", CultureInfo.InvariantCulture)
5655
: fractionalDigits.ToString("G", CultureInfo.InvariantCulture)
5756
)

0 commit comments

Comments
 (0)