Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Develop #8

Open
wants to merge 11 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions .github/ISSUE_TEMPLATE/bug_report.md

This file was deleted.

20 changes: 0 additions & 20 deletions .github/ISSUE_TEMPLATE/feature_request.md

This file was deleted.

17 changes: 0 additions & 17 deletions .github/workflows/UnitTests.yml

This file was deleted.

20 changes: 0 additions & 20 deletions .github/workflows/dotnetcore.yml

This file was deleted.

13 changes: 12 additions & 1 deletion ConsoleTest/ConsoleTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,20 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.0</TargetFramework>
<TargetFramework>netcoreapp3.1</TargetFramework>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<DefineConstants>DEBUG;TRACE</DefineConstants>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.8">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\NumericWordsConversion\NumericWordsConversion.csproj" />
</ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions ConsoleTest/ConsoleTest.csproj.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:String x:Key="/Default/CodeInspection/CSharpLanguageProject/LanguageLevel/@EntryValue">CSharp80</s:String>
<s:Boolean x:Key="/Default/Housekeeping/ProjectModelSynchronizer/ProjectGuidsToAlwaysCallMsbuild/=FE38D91CE92C3F48A4BB9E099BA7D76F/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
6 changes: 3 additions & 3 deletions ConsoleTest/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ class Program
static void Main(string[] args)
{

CurrencyWordsConverter converter = new CurrencyWordsConverter();
decimal number = 123_000M;
string words = converter.ToWords(number);
var converter = new CurrencyWordsConverter();
const Decimal number = 123_000M;
var words = converter.ToWords(number);
//words = number.To
Console.WriteLine(words);
Console.ReadKey();
Expand Down
177 changes: 93 additions & 84 deletions NumericWordsConversion/ConversionFactory.cs
Original file line number Diff line number Diff line change
@@ -1,121 +1,130 @@
using System;
using System.Globalization;
using System.Text;
using static System.String;

namespace NumericWordsConversion
{
/// <summary>
/// Used to simply map numbers with their respective words output as per the specified options
/// </summary>
internal class ConversionFactory
{
private readonly NumericWordsConversionOptions _options;
namespace NumericWordsConversion {

using System;
using System.Globalization;
using System.Text;
using static System.String;

/// <summary>Used to simply map numbers with their respective words output as per the specified options</summary>
internal class ConversionFactory {

private readonly string[] _ones;
private readonly string[] _tens;

private readonly NumericWordsConversionOptions _options;

private readonly string[] _scale;

public ConversionFactory(NumericWordsConversionOptions options)
{
_options = options;
Utilities.ManageSuitableResources(out _ones, out _tens, out _scale, options);
private readonly string[] _tens;

public ConversionFactory( NumericWordsConversionOptions options ) {
this._options = options;
Utilities.ManageSuitableResources( out this._ones, out this._tens, out this._scale, options );
}
public ConversionFactory(NumericWordsConversionOptions options, string[] ones, string[] tens, string[] scale)
{
_options = options;
_ones = ones;
_tens = tens;
_scale = scale;

public ConversionFactory( NumericWordsConversionOptions options, string[] ones, string[] tens, string[] scale ) {
this._options = options;
this._ones = ones;
this._tens = tens;
this._scale = scale;
}

internal string ToOnesWords(ushort digit) => _ones[digit];
internal string ToOnesWords( ushort digit ) => this._ones[ digit ];

internal string ToTensWord( string tenth ) {
int dec = Convert.ToUInt16( tenth, CultureInfo.InvariantCulture );

if ( dec <= 0 ) {
return Empty;
}

internal string ToTensWord(string tenth)
{
int dec = Convert.ToUInt16(tenth, CultureInfo.InvariantCulture);
if (dec <= 0) return Empty;
string words;

if (dec < _options.ResourceLimitIndex)
{
words = _ones[dec];
if ( dec < this._options.ResourceLimitIndex ) {
words = this._ones[ dec ];
}
else
{
if (dec % 10 == 0)
{
words = _tens[dec / 10];
else {
if ( dec % 10 == 0 ) {
words = this._tens[ dec / 10 ];
}
else
{
int first = Convert.ToUInt16(tenth.Substring(0, 1), CultureInfo.InvariantCulture);
int second = Convert.ToUInt16(tenth.Substring(1, 1), CultureInfo.InvariantCulture);
words = Concat(_tens[first], " ", _ones[second]);
else {
int first = Convert.ToUInt16( tenth.Substring( 0, 1 ), CultureInfo.InvariantCulture );
int second = Convert.ToUInt16( tenth.Substring( 1, 1 ), CultureInfo.InvariantCulture );
words = Concat( this._tens[ first ], " ", this._ones[ second ] );
}
}

return words;
}

internal string ToHundredthWords(string hundred)
{
string inWords = Empty;
if (hundred.Length == 3)
{
int hundredth = Convert.ToInt16(hundred.Substring(0, 1), CultureInfo.InvariantCulture);
inWords = hundredth > 0 ? Concat(_ones[hundredth], " ", _scale[1], " ") : Empty;
hundred = hundred.Substring(1, 2);
internal string ToHundredthWords( string hundred ) {
var inWords = Empty;

if ( hundred.Length == 3 ) {
int hundredth = Convert.ToInt16( hundred.Substring( 0, 1 ), CultureInfo.InvariantCulture );
inWords = hundredth > 0 ? Concat( this._ones[ hundredth ], " ", this._scale[ 1 ], " " ) : Empty;
hundred = hundred.Substring( 1, 2 );
}
inWords += ToTensWord(hundred);

inWords += this.ToTensWord( hundred );

return inWords.Trim();
}

/// <summary>
/// Responsible for converting any input digits to words
/// </summary>
/// <summary>Responsible for converting any input digits to words</summary>
/// <param name="digits"></param>
/// <returns></returns>
internal string ConvertDigits(string digits)
{
if (digits == "0") return _ones[0];
StringBuilder builder = new StringBuilder();
internal string ConvertDigits( string digits ) {
if ( digits == "0" ) {
return this._ones[ 0 ];
}

var builder = new StringBuilder();
int scaleMapIndex;
if (_options.Culture == Culture.International)
scaleMapIndex = (int)Math.Ceiling((decimal)digits.Length / 3);
else
scaleMapIndex = (digits.Length - 3) < 1 ? 1 : digits.Length / 2;
for (int i = scaleMapIndex; i > 0; i--)
{

if ( this._options.Culture == Culture.International ) {
scaleMapIndex = ( int ) Math.Ceiling( ( decimal ) digits.Length / 3 );
}
else {
scaleMapIndex = digits.Length - 3 < 1 ? 1 : digits.Length / 2;
}

for ( var i = scaleMapIndex; i > 0; i-- ) {
string inWords;
switch (i)
{

switch ( i ) {
case 1: //For the Hundreds, tens and ones
inWords = ToHundredthWords(digits);
if (!IsNullOrEmpty(inWords))
builder.Append(Concat(inWords.Trim(), " "));
inWords = this.ToHundredthWords( digits );

if ( !IsNullOrEmpty( inWords ) ) {
builder.Append( Concat( inWords.Trim(), " " ) );
}

break;
default: //For Everything Greater than hundreds
if (_options.Culture == Culture.International)
{
int length = (digits.Length % ((i - 1) * 3 + 1)) + 1;
string hundreds = digits.Substring(0, length);
digits = digits.Remove(0, length);
inWords = ToHundredthWords(hundreds);
if ( this._options.Culture == Culture.International ) {
var length = (digits.Length % ( (( i - 1 ) * 3) + 1 )) + 1;
var hundreds = digits.Substring( 0, length );
digits = digits.Remove( 0, length );
inWords = this.ToHundredthWords( hundreds );
}
else {
var length = digits.Length % 2 == 0 ? 1 : 2;
var hundreds = digits.Substring( 0, length );
digits = digits.Remove( 0, length );
inWords = this.ToTensWord( hundreds );
}
else
{
int length = (digits.Length % 2 == 0) ? 1 : 2;
string hundreds = digits.Substring(0, length);
digits = digits.Remove(0, length);
inWords = ToTensWord(hundreds);

if ( !IsNullOrEmpty( inWords.Trim() ) ) {
builder.Append( Concat( inWords.Trim(), " ", this._scale[ i ], " " ) );
}

if (!IsNullOrEmpty(inWords.Trim()))
builder.Append(Concat(inWords.Trim(), " ", _scale[i], " "));
break;
}
}

return builder.ToString().Trim();
}

}
}

}
Loading