为加密货币四舍五入到货币字符串输出
当前的情况和问题
我目前有一个问题,可以以有意义的方式在我的钱包中显示可用的加密货币数量。 问题在于,加密货币的USD当量存在很大的不同,因此所需的精确度有很大变化。
例如,可以说我在钱包的任何给定的加密货币中都有50美元的价值。在撰写本文时,这看起来如下:
货币 | SHIB | USDT | BTC |
---|---|---|---|
金额(每个50美元) | 6273525.72145 ... | 50 | 0.0023707 |
现有解决方案(用于经典货币)
,可以说我去购买经典的货币格式:货币什台
currency.ToString("0.##");
USDT | 货币 | BTC | 金额 |
---|---|---|---|
6273525.72 | 50.00 | 0.00 | 0.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:
currency | shib | usdt | btc |
---|---|---|---|
amount (50 usd each) | 6273525.72145... | 50 | 0.0023707 |
in a real world example, it looks like the following:
existing solution (for classic currencies)
Lets say I go for a classic formatting of currencies:
currency.ToString("0.##");
currency | shib | usdt | btc |
---|---|---|---|
amount | 6273525.72 | 50.00 | 0.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:
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不确定这是一种很好的方法,您可能会寻找正则。
带有样本测试数据的正则态度101
上面的正则判处捕获了群组的数量,该模式的模式以“ 0”开头是“ 0” 。”,具有零或更多“ 0”或更多的数字字符,至少有1个且最多3个出现。
之所以使用格式模式将
double
转换为字符串
,以便输出字符串不会具有E(指数)。而
.trimend('0')
是由于先前的格式而在后面删除额外的零。例如:
sample .net小提琴
Unsure this is a good approach or not, you may look for regex.
Regex 101 with Sample Test Data
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.
The reason why is to convert
double
tostring
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:
Sample .NET Fiddle