Skip to content

Commit 294e188

Browse files
XRPL Currency value string representations (#22)
1 parent c1513ac commit 294e188

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/XUMM.NET.SDK/Extensions/CurrencyExtensions.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System;
2+
using System.Globalization;
23
using System.Linq;
34
using System.Text;
45
using System.Text.RegularExpressions;
@@ -59,6 +60,25 @@ public static string ToFormattedCurrency(this string currency, int maxLength = 1
5960
return "???";
6061
}
6162

63+
/// <summary>
64+
/// XRP Ledger APIs generally use strings, rather than native JSON numbers, to represent numeric amounts of currency for both XRP and tokens.
65+
/// This protects against a loss of precision when using JSON parsers, which may automatically try to represent all JSON numbers in a floating-point format.
66+
/// For more detailed information, see the currency format reference.
67+
/// <seealso href="https://xrpl.org/currency-formats.html#string-numbers" />
68+
/// </summary>
69+
/// <param name="value">XRPL currency value as a string representation.</param>
70+
/// <exception cref="FormatException">Thrown when <paramref name="value"/> can't be parsed to a decimal.</exception>
71+
/// <returns>Returns the decimal value of the string representation.</returns>
72+
public static decimal XrplStringNumberToDecimal(this string value)
73+
{
74+
if (!decimal.TryParse(value, NumberStyles.Float, CultureInfo.InvariantCulture, out var result))
75+
{
76+
throw new FormatException($"Unable to convert string number \"{value}\" to a decimal.");
77+
}
78+
79+
return result;
80+
}
81+
6282
/// <summary>
6383
/// In technical contexts, XRP is measured precisely to the nearest 0.000001 XRP, called a "drop" of XRP. The rippled APIs require all XRP amounts to be specified in drops of XRP.
6484
/// For more detailed information, see the currency format reference.

0 commit comments

Comments
 (0)