Skip to content

Commit

Permalink
culture info taken into account when parsing decimal | #10
Browse files Browse the repository at this point in the history
  • Loading branch information
Samyeak committed Jan 3, 2024
1 parent 124b9ca commit 1714c05
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 57 deletions.
16 changes: 8 additions & 8 deletions NumericWordsConversion.sln
Original file line number Diff line number Diff line change
@@ -1,24 +1,20 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.29509.3
# Visual Studio Version 17
VisualStudioVersion = 17.8.34330.188
MinimumVisualStudioVersion = 10.0.40219.1
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumericWordsConversion", "NumericWordsConversion\NumericWordsConversion.csproj", "{85861DC0-DE30-4D37-9C07-FAFE66409A47}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "UnitTests", "UnitTests\UnitTests.csproj", "{06BB2AD6-C600-4221-8E21-2EBDDFECF31B}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ConsoleTest", "ConsoleTest\ConsoleTest.csproj", "{1CD938FE-2CE9-483F-A4BB-9E099BA7D76F}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumericWordsConversion", "NumericWordsConversion\NumericWordsConversion.csproj", "{457F6EE6-B519-4B02-A17A-B58AE34D8CD2}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{85861DC0-DE30-4D37-9C07-FAFE66409A47}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{85861DC0-DE30-4D37-9C07-FAFE66409A47}.Debug|Any CPU.Build.0 = Debug|Any CPU
{85861DC0-DE30-4D37-9C07-FAFE66409A47}.Release|Any CPU.ActiveCfg = Release|Any CPU
{85861DC0-DE30-4D37-9C07-FAFE66409A47}.Release|Any CPU.Build.0 = Release|Any CPU
{06BB2AD6-C600-4221-8E21-2EBDDFECF31B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{06BB2AD6-C600-4221-8E21-2EBDDFECF31B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{06BB2AD6-C600-4221-8E21-2EBDDFECF31B}.Release|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -27,6 +23,10 @@ Global
{1CD938FE-2CE9-483F-A4BB-9E099BA7D76F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{1CD938FE-2CE9-483F-A4BB-9E099BA7D76F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{1CD938FE-2CE9-483F-A4BB-9E099BA7D76F}.Release|Any CPU.Build.0 = Release|Any CPU
{457F6EE6-B519-4B02-A17A-B58AE34D8CD2}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{457F6EE6-B519-4B02-A17A-B58AE34D8CD2}.Debug|Any CPU.Build.0 = Debug|Any CPU
{457F6EE6-B519-4B02-A17A-B58AE34D8CD2}.Release|Any CPU.ActiveCfg = Release|Any CPU
{457F6EE6-B519-4B02-A17A-B58AE34D8CD2}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
6 changes: 3 additions & 3 deletions NumericWordsConversion/ConversionFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ internal string ConvertDigits(string digits)
if (_options.Culture == Culture.International)
scaleMapIndex = (int)Math.Ceiling((decimal)digits.Length / 3);
else
scaleMapIndex = (digits.Length - 3) < 1 ? 1 : digits.Length / 2;
scaleMapIndex = digits.Length - 3 < 1 ? 1 : digits.Length / 2;
for (int i = scaleMapIndex; i > 0; i--)
{
string inWords;
Expand All @@ -97,14 +97,14 @@ internal string ConvertDigits(string digits)
default: //For Everything Greater than hundreds
if (_options.Culture == Culture.International)
{
int length = (digits.Length % ((i - 1) * 3 + 1)) + 1;
int length = digits.Length % ((i - 1) * 3 + 1) + 1;
string hundreds = digits.Substring(0, length);
digits = digits.Remove(0, length);
inWords = ToHundredthWords(hundreds);
}
else
{
int length = (digits.Length % 2 == 0) ? 1 : 2;
int length = digits.Length % 2 == 0 ? 1 : 2;
string hundreds = digits.Substring(0, length);
digits = digits.Remove(0, length);
inWords = ToTensWord(hundreds);
Expand Down
18 changes: 9 additions & 9 deletions NumericWordsConversion/Culture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
/// </summary>
public enum Culture
{
/// <summary>
/// Uses the International Numeral System for Words Conversion
/// </summary>
/// <summary>
/// Uses the International Numeral System for Words Conversion
/// </summary>
International,
/// <summary>
/// Uses the Nepalese Numeral System for Words Conversion
/// </summary>
/// <summary>
/// Uses the Nepalese Numeral System for Words Conversion
/// </summary>
Nepali,
/// <summary>
/// Uses the Hindi Numeral System for Words Conversion
/// </summary>
/// <summary>
/// Uses the Hindi Numeral System for Words Conversion
/// </summary>
Hindi
}
}
10 changes: 5 additions & 5 deletions NumericWordsConversion/CurrencyWordsConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class CurrencyWordsConverter
/// </summary>
public CurrencyWordsConverter()
{
this._options = GlobalOptions.CurrencyWordsOptions;
_options = GlobalOptions.CurrencyWordsOptions;
_conversionFactory = Utilities.InitializeConversionFactory(_options);
}

Expand All @@ -25,7 +25,7 @@ public CurrencyWordsConverter()
/// </summary>
public CurrencyWordsConverter(CurrencyWordsConversionOptions options)
{
this._options = options;
_options = options;
_conversionFactory = Utilities.InitializeConversionFactory(_options);
}
#endregion
Expand All @@ -47,18 +47,18 @@ public string ToWords(decimal number)
CultureInfo.InvariantCulture)
.Split('.')
.ElementAtOrDefault(1) ?? Empty;
if (decimal.Parse(integralDigitsString) <= 0 && decimal.Parse(fractionalDigitsString) <= 0) return Empty;
if (decimal.Parse(integralDigitsString, CultureInfo.InvariantCulture) <= 0 && decimal.Parse(fractionalDigitsString, CultureInfo.InvariantCulture) <= 0) return Empty;

string integralWords = Empty;
if (decimal.Parse(integralDigitsString) > 0)
if (decimal.Parse(integralDigitsString, CultureInfo.InvariantCulture) > 0)
{
integralWords = _conversionFactory.ConvertDigits(integralDigitsString);
integralWords = _options.CurrencyNotationType == NotationType.Prefix
? _options.CurrencyUnit + " " + integralWords
: integralWords + " " + _options.CurrencyUnit;
}

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

string fractionalWords = _conversionFactory.ConvertDigits(fractionalDigitsString);
fractionalWords = _options.SubCurrencyNotationType == NotationType.Prefix
Expand Down
2 changes: 1 addition & 1 deletion NumericWordsConversion/NumericWordsConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public static class GlobalOptions
public static NumericWordsConverter NumericWordsConverter { get; internal set; } = new NumericWordsConverter(NumericWordsOptions);

public static CurrencyWordsConverter CurrencyWordsConverter { get; internal set; } = new CurrencyWordsConverter(CurrencyWordsOptions);
}
}

public class OptionsInitializer
{
Expand Down
29 changes: 4 additions & 25 deletions NumericWordsConversion/NumericWordsConversion.csproj
Original file line number Diff line number Diff line change
@@ -1,35 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.0</TargetFramework>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Authors>Samyeak Maharjan</Authors>
<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>
<Copyright>Samyeak Maharjan</Copyright>
<NeutralLanguage>en</NeutralLanguage>
<Version>2.0.0.0</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
<PackageProjectUrl>https://github.com/Samyeak/NumericWordsConversion.git</PackageProjectUrl>
<RepositoryUrl>https://github.com/Samyeak/NumericWordsConversion.git</RepositoryUrl>
<RepositoryType>github</RepositoryType>
<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>
<PackageReleaseNotes>* Added Application Level Configuration
* Added Hindi Numeral System</PackageReleaseNotes>
<SignAssembly>false</SignAssembly>
<DelaySign>false</DelaySign>
<AssemblyOriginatorKeyFile>NumericWordsConversionSignatureKey.snk</AssemblyOriginatorKeyFile>
<PackageRequireLicenseAcceptance>false</PackageRequireLicenseAcceptance>
<PackageIconUrl>https://repository-images.githubusercontent.com/233162696/d06bb100-4545-11ea-8853-f816e055791a</PackageIconUrl>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<Optimize>true</Optimize>
<TargetFramework>netstandard2.1</TargetFramework>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<None Include="..\LICENSE">
<Pack>True</Pack>
<None Include="..\LICENSE" Link="LICENSE">
<PackagePath></PackagePath>
<Pack>True</Pack>
</None>
</ItemGroup>

Expand Down
2 changes: 1 addition & 1 deletion NumericWordsConversion/NumericWordsConversionOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,6 @@ public string DecimalSeparator
/// In order to use generic algorithm for all the numeral system,
/// this is used to map suitable resources as per different numeral system
/// </summary>
internal int ResourceLimitIndex => this.OutputFormat == OutputFormat.English ? 20 : 100;
internal int ResourceLimitIndex => OutputFormat == OutputFormat.English ? 20 : 100;
}
}
9 changes: 4 additions & 5 deletions NumericWordsConversion/NumericWordsConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class NumericWordsConverter
/// </summary>
public NumericWordsConverter()
{
this._options = GlobalOptions.NumericWordsOptions;
_options = GlobalOptions.NumericWordsOptions;
_conversionFactory = Utilities.InitializeConversionFactory(_options);
}

Expand All @@ -31,7 +31,7 @@ public NumericWordsConverter()
/// </summary>
public NumericWordsConverter(NumericWordsConversionOptions options)
{
this._options = options;
_options = options;
_conversionFactory = Utilities.InitializeConversionFactory(_options);
}
#endregion
Expand All @@ -45,13 +45,12 @@ public string ToWords(decimal number)
{
string integralDigitsString = number
.ToString(CultureInfo.InvariantCulture)
.Split('.')
.ElementAt(0);
.Split('.')[0];

decimal fractionalDigits = number % 1;

string integralWords = _conversionFactory.ConvertDigits(integralDigitsString);
string fractionalDigitsString = (_options.DecimalPlaces > -1 ? decimal.Parse(fractionalDigits.ToString($"F{_options.DecimalPlaces}", CultureInfo.InvariantCulture))
string fractionalDigitsString = (_options.DecimalPlaces > -1 ? decimal.Parse(fractionalDigits.ToString($"F{_options.DecimalPlaces}", CultureInfo.InvariantCulture), CultureInfo.InvariantCulture)
.ToString($"G{_options.DecimalPlaces}", CultureInfo.InvariantCulture)
: fractionalDigits.ToString("G", CultureInfo.InvariantCulture)
)
Expand Down

0 comments on commit 1714c05

Please sign in to comment.