Skip to content

Commit

Permalink
chore: Modify input form of exchange rate
Browse files Browse the repository at this point in the history
  • Loading branch information
OnedgeLee committed Jan 26, 2025
1 parent 5159e19 commit eba7b98
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 2 deletions.
3 changes: 1 addition & 2 deletions Lib9c/TableData/Swap/SwapRateSheet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@ public override void Set(IReadOnlyList<string> fields)
var currencyFrom = ParseCurrency(fields[0]);
var currencyTo = ParseCurrency(fields[1]);
Pair = new CurrencyPair(currencyFrom, currencyTo);
RateNumerator = ParseBigInteger(fields[2]);
RateDenominator = ParseBigInteger(fields[3]);
(RateNumerator, RateDenominator) = ParseFraction(fields[2]);
}
}

Expand Down
34 changes: 34 additions & 0 deletions Lib9c/TableData/TableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,40 @@ public static BigInteger ParseBigInteger(string value)
throw new ArgumentException(value);
}

public static (BigInteger Numerator, BigInteger Denominator) ParseFraction(string value)
{
if (TryParseFraction(value, out var result))
{
return result;
}

throw new ArgumentException(value);
}

public static bool TryParseFraction(string value, out (BigInteger Numerator, BigInteger Denominator) result)
{
result = (BigInteger.One, BigInteger.One);

List<string> fractionStrings = value.Split("/").ToList();
if (fractionStrings.Count != 2)
{
return false;
}

if (!BigInteger.TryParse(fractionStrings[0].Trim(), out var numerator))
{
return false;
}

if (!BigInteger.TryParse(fractionStrings[1].Trim(), out var denominator))
{
return false;
}

result = (numerator, denominator);
return true;
}

public static Currency ParseCurrency(string value)
{
if (TryParseCurrency(value, out var result))
Expand Down

0 comments on commit eba7b98

Please sign in to comment.