- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如果货币金额非常大,我会尝试将其缩写。
例如:
if (amt > 1000000)
{
decimal d = (decimal)Math.Round(amt / 1000, 0);
return String.Format("{0:C0}", d) + " K";
}
如果给定的数字超过 100 万,它将去掉最后 3 位数字并替换为 K。当货币符号(如 $ 在左侧)时工作正常
但是,一些货币符号会放在右侧。
因此,我不会用漂亮的 $100 K 换取美元,而是用 100 € K 换取法国欧元。
如何更改格式以将 K 紧跟在数字之后和货币符号之前。
这似乎有点过头了。有什么想法吗?
最佳答案
我会像这样用 IFormatProvider 创建一个类
public class MoneyFormat: IFormatProvider, ICustomFormatter
{
public object GetFormat(Type formatType)
{
if (formatType == typeof(ICustomFormatter))
return this;
else
return null;
}
public string Format(string fmt, object arg, IFormatProvider formatProvider)
{
if (arg.GetType() != typeof(decimal))
try
{
return HandleOtherFormats(fmt, arg);
}
catch (FormatException e)
{
throw new FormatException(string.Format("The format of '{0}' is invalid", fmt), e);
}
string ufmt = fmt.ToUpper(CultureInfo.InvariantCulture);
if (!(ufmt == "K"))
try
{
return HandleOtherFormats(fmt, arg);
}
catch (FormatException e)
{
throw new FormatException(string.Format("The format of '{0}' is invalid", fmt), e);
}
decimal result;
if (decimal.TryParse(arg.ToString(), out result))
{
if (result >= 1000000)
{
decimal d = (decimal)Math.Round(result / 10000, 0);
CultureInfo clone = (CultureInfo)CultureInfo.CurrentCulture.Clone();
string oldCurrSymbol = clone.NumberFormat.CurrencySymbol;
clone.NumberFormat.CurrencySymbol = "";
return String.Format(clone, "{0:C0}", d).Trim() + " K" + oldCurrSymbol;
}
}
else
return string.Format("{0:C0}", result) + " K";
}
private string HandleOtherFormats(string format, object arg)
{
if (arg is IFormattable)
return ((IFormattable)arg).ToString(format, CultureInfo.CurrentCulture);
else if (arg != null)
return arg.ToString();
else
return string.Empty;
}
}
然后你可以像这样以你的格式调用它:
return string.Format( new MoneyFormat(), "{0:K}", amt);
然后您可以调整您想要表示“K”或其他要添加的引用符号的方式
文化信息(“fr-fr”):10 万欧元
文化信息(“en-us”):100 千美元
文化信息(“ru-RU”):100 Kр。
关于c# - K、M 和 B 的 String.Format 货币,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29091240/
条件: 我们需要我们 Magento 网站的默认显示货币是美元,Paypal(基础货币)也需要是美元(因为我们需要客户以美元支付)。 我们的 Fedex a/c 是在新加坡开设的,结果发现运费是以新加
根据 currency_str 和 created_date_time 合并两个数据帧(xrate 和 df)时遇到问题 display(xrate.info()) Int64Index: 1611
我必须匹配像 这样的值 € 6.483,00 或类似的值 18,50% 或者,再次, +65,86 % 在我起草的 Javascript 函数中: function(s) { return /^
我正在尝试将货币金额解析并存储为 BigDecimal 值。我得到了相关货币的区域设置,在大多数情况下它工作正常,但当货币是哥斯达黎加科朗时我得到了意想不到的结果。 我的哥斯达黎加客户告诉我,典型的货
在当前版本的 Excel(Office 365/2019 年 2 月)中获得近似每日外汇汇率的最简单的免费方法是什么。 我对历史数据不感兴趣,我只想要从货币 X 到货币 Y 的最后已知日汇率,并且只在
我在 spree 时遇到了麻烦,不知道如何处理。 我无法更改主要货币。 我该怎么做? 最佳答案 它在 Spree 2.0.0 中更容易,而且显然它也可以在以前的 spree 版本中工作。 转到您的 c
在我的网络应用程序中,我有一个名为“预算”的输入字段,用户可以在其中输入项目的建议预算。我需要创建一个屏蔽输入,以便在用户在输入字段中输入时自动将输入的金额转换为以下格式: 1000 10 000 1
我正在从数据库返回一个字符串,但由于数据库的编码方式,一些英镑符号 (£) 被问号取代。我想恢复井号,但不替换字符串中真正的问号。我已经设法编写了一个正则表达式来测试问号后跟数字的组合,但我不确定如何
我在使用亚马逊销售 API 时遇到问题。我在 amazon.co.uk 有一个帐户,可以正常发送产品。我在 amazon.de 有一个新帐户,除了货币外,一切都很好。 我有一个零售价为 10 英镑 (
Pharo 有什么方法可以将数字转换为单词。 例如:1200 = 一千二百而已。 实现起来并不难,只是想知道。 最佳答案 Integer>>asWords会这样做。 1200 asWords返回 'o
我有一个简单的单页网络应用程序。它从两个不同的 API 获取货币数据(以美元为单位),在成功检索后,promise 被解决,一些计算得出 GBP/Bitcoin 汇率。 我正试图找到一种干净的方法,然
我有以下简单的计算,它将两个值加在一起。这些值与使用“R”作为前缀标识的兰特(南非货币)有关。 function calculate() { var A = parseFloat(docume
使用 JavaScript,什么是将点替换为逗号的正确方法(对于欧盟货币),例如: 2000.65 将是 2000,65 而不是 2,000.65 39.20 将是 39,20 我不确定 cost.r
我在文本框中显示带有 NumberFormat 的货币符号 NumberFormat numberFormat = NumberFormat.getSimpleCurrencyFo
我已经设法从非接触式阅读器读取了一个交易事件,使用 现在我的 Activity 打开了,我被困在那个点上,因为我认为我的 Intent 中会有一些数据,比如 amou
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 Improve th
这个问题在这里已经有了答案: Why not use Double or Float to represent currency? (16 个答案) 关闭 3 年前。 我的应用程序中的一些计算有问题
我使用 Jruby(反正就是 ruby,在 jvm 下运行 :D )和马拉松测试(一个 java swing 应用程序),我在处理货币数字时遇到了一些麻烦。 我不使用 Rails(不知道我是否可以
我正在尝试删除 、 或 之后的尾随零。 以一种货币表示,仍然保留货币符号。例如,€90.00 到 €90、90.00€ 到 90€ 和 €90.33 到 €90.33。 示例如下: $('.produ
我想将带小数点的数字(以货币计)转换成单词 例如:12345.60--> 一万二千三百四十五美元六十美分 我从 http://www.csharp-tutorials.info/2016/04/con
我是一名优秀的程序员,十分优秀!