Skip to content

Commit

Permalink
Adding ToBase36Number and ToBase62Number
Browse files Browse the repository at this point in the history
  • Loading branch information
juanonsoftware committed Nov 28, 2023
1 parent f6a7ccc commit 37a4ab6
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
46 changes: 46 additions & 0 deletions BigNumber/BigNumberExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.IO;
using System.Text;

namespace BigNumber
{
Expand Down Expand Up @@ -34,6 +36,28 @@ public static Base36Number ToBase36Number(this int base10Number)
return new Base36Number(BitConverter.GetBytes(base10Number));
}

/// <summary>
/// New Base36Number from base-10 number
/// </summary>
/// <param name="base10Number"></param>
/// <returns></returns>
public static Base36Number ToBase36Number(this decimal base10Number)
{
var bytes = Encoding.ASCII.GetBytes(base10Number.ToString());
return new Base36Number(bytes);
}

/// <summary>
/// Get back a base-10 decimal number from Base36Number
/// </summary>
/// <param name="base36"></param>
/// <returns></returns>
public static decimal AsDecimal(this Base36Number base36)
{
var str = Encoding.ASCII.GetString(base36.IntValue.ToByteArray());
return decimal.Parse(str);
}

/// <summary>
/// New Base62Number from Guid instance
/// </summary>
Expand Down Expand Up @@ -63,5 +87,27 @@ public static Base62Number ToBase62Number(this int base10Number)
{
return new Base62Number(BitConverter.GetBytes(base10Number));
}

/// <summary>
/// New Base62Number from base-10 number
/// </summary>
/// <param name="base10Number"></param>
/// <returns></returns>
public static Base62Number ToBase62Number(this decimal base10Number)
{
var bytes = Encoding.ASCII.GetBytes(base10Number.ToString());
return new Base62Number(bytes);
}

/// <summary>
/// Get back a base-10 decimal number from Base62Number
/// </summary>
/// <param name="base62"></param>
/// <returns></returns>
public static decimal AsDecimal(this Base62Number base62)
{
var str = Encoding.ASCII.GetString(base62.IntValue.ToByteArray());
return decimal.Parse(str);
}
}
}
14 changes: 14 additions & 0 deletions BigNumberTests/Base36NumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ public void CanCreateNewBase36NumberFromLongNumber()
Assert.IsTrue(number.IntValue > 0);
}

[TestMethod]
public void CanCreateNewBase36NumberFromDecimalNumber()
{
var number = ((decimal)1000.1).ToBase36Number();
Assert.IsTrue(number.IntValue > 0);
}

[TestMethod]
public void CanGetDecimalFromABase36NumberOfADecimal()
{
var number = (10M).ToBase36Number();
Assert.AreEqual(10, number.AsDecimal());
}

[TestMethod]
public void CanCreateNewBase36NumberFromString_02()
{
Expand Down
14 changes: 14 additions & 0 deletions BigNumberTests/Base62NumberTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ public void CanCreateNewBase62NumberFromLongNumber()
Assert.IsTrue(number.IntValue > 0);
}

[TestMethod]
public void CanCreateNewBase62NumberFromDecimalNumber()
{
var number = ((decimal)1000.1).ToBase62Number();
Assert.IsTrue(number.IntValue > 0);
}

[TestMethod]
public void CanGetDecimalFromABase62NumberOfADecimal()
{
var number = (10M).ToBase62Number();
Assert.AreEqual(10, number.AsDecimal());
}

[TestMethod]
public void CanCreateNewBase62NumberFromString_02()
{
Expand Down

0 comments on commit 37a4ab6

Please sign in to comment.