为加密货币四舍五入到货币字符串输出

发布于 2025-02-08 06:46:10 字数 1576 浏览 1 评论 0原文

当前的情况和问题

我目前有一个问题,可以以有意义的方式在我的钱包中显示可用的加密货币数量。 问题在于,加密货币的USD当量存在很大的不同,因此所需的精确度有很大变化。

例如,可以说我在钱包的任何给定的加密货币中都有50美元的价值。在撰写本文时,这看起来如下:

货币SHIBUSDTBTC
金额(每个50美元)6273525.72145 ...500.0023707

在现实世界中,看起来如下:

现有解决方案(用于经典货币)

,可以说我去购买经典的货币格式:货币什台

currency.ToString("0.##");
USDT货币BTC金额
6273525.7250.000.000.00 0.00

,而这对货币的货币(如货币)很高,货币,货币,货币较高,诸如BTC或ETH之类的价值将有问题:

伪代码/解决方案

我认为,在逗号之后,使用3位精度的第一个有意义的数字是可行的:

if (amount >= 1)
{ // in a range to show 2 decimals
    string result = amount.ToString("0.##"); // eg 5.93
}
else
{ // truncate to precision of 3
    // 0.0184432468325 -> "0.0184"
    // 0.00237392482309 -> "0.00237"
    // 0.00043930984274 -> "0.000439"
    // 0.00000000001279 -> "0.0000000000127"
    // 0.123000004234 -> "0.123"
}

我将如何进行最后一部分(如果金额较小,则是0) ?

current situation and issue

I currently have an issue to display the available amount of a crypto currency in my wallet in a meaningful way.
the issue is that the usd equivalents of the cryptos are differing immensly and thus the required precisions are varying a lot.

For instance, lets say I have 50 USD worth in any given cryptocurrency of my wallet. At the time of writing, this would look like the following:

currencyshibusdtbtc
amount (50 usd each)6273525.72145...500.0023707‬

in a real world example, it looks like the following:
enter image description here

existing solution (for classic currencies)

Lets say I go for a classic formatting of currencies:

currency.ToString("0.##");
currencyshibusdtbtc
amount6273525.7250.000.00

while this would work great for currencies such as shib or usdt, currencies with higher value such as btc or eth will have problems with this:
enter image description here

pseudo-code/solution

I think it would be feasible to go with 3 digits of precision for the first significant digit after the comma:

if (amount >= 1)
{ // in a range to show 2 decimals
    string result = amount.ToString("0.##"); // eg 5.93
}
else
{ // truncate to precision of 3
    // 0.0184432468325 -> "0.0184"
    // 0.00237392482309 -> "0.00237"
    // 0.00043930984274 -> "0.000439"
    // 0.00000000001279 -> "0.0000000000127"
    // 0.123000004234 -> "0.123"
}

how would I go about the last part (if amount is smaller 0)?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

々眼睛长脚气 2025-02-15 06:46:10

不确定这是一种很好的方法,您可能会寻找正则。

带有样本测试数据的正则态度101

^(0.0*\d{1,3})

上面的正则判处捕获了群组的数量,该模式的模式以“ 0”开头是“ 0” 。”,具有零或更多“ 0”或更多的数字字符,至少有1个且最多3个出现。

public static string TruncateSmallerZeroCryptoAmt(double amount)
{
    Regex regex = new Regex("^(0.0*\\d{3})");
    var match = regex.Match(String.Format("{0:F20}", amount));
        
    return match.Value.TrimEnd('0');
}

之所以使用格式模式将double转换为字符串,以便输出字符串不会具有E(指数)。

.trimend('0')是由于先前的格式而在后面删除额外的零。

例如:

0.300 - >显示为0.3

sample .net小提琴

Unsure this is a good approach or not, you may look for regex.

Regex 101 with Sample Test Data

^(0.0*\d{1,3})

The above regex captures the group amount with a pattern starting with "0.", has zero or more "0" and lastly any numeric characters with at least 1 and up to 3 occurrences.

public static string TruncateSmallerZeroCryptoAmt(double amount)
{
    Regex regex = new Regex("^(0.0*\\d{3})");
    var match = regex.Match(String.Format("{0:F20}", amount));
        
    return match.Value.TrimEnd('0');
}

The reason why is to convert double to string with a formatting pattern so that the output string will not have E (exponential).

While .TrimEnd('0') is to remove the extra zeros at the back due to previous formatting.

For example:

0.300 --> Display as 0.3

Sample .NET Fiddle

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文