c# - String.Format 函数不起作用

标签 c# asp.net string string-formatting

这似乎是一个非常愚蠢的问题,但我还没有设法解决它。这是代码:

private string[] ConvertToCurrency(string[] costs)
{
    int count = costs.Length;

    for (int i = 0; i < count - 1; i++)
    {
        costs[i] = String.Format("{0:C}", costs[i]);
    }

    return costs;
}

我希望输出应该是我存储在我的字符串数组中的数字将被格式化为货币,但当它们从另一端出来时它们完全没有变化。

我不知道为什么会这样,并尝试了一些其他方法来格式化它,但没有成功。

最佳答案

您应该这样做,{0:C} 用于货币格式,它将应用于数值。

costs[i] = String.Format("{0:C}", Convert.ToDecimal(costs[i]));

关于c# - String.Format 函数不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13191619/

相关文章:

javascript - 如何将json日期格式转换为普通日期格式。例如 :'/Date(1388412591038)/' 到 '12-30-2013' ?

html - 使用 ASP 重复器创建一个 HTML 表格,水平重复

c++ - 如何打印出使用数组删除某些字符的回文?

c# - 如何从字符串中删除 "\"

c# - "System.Windows.Forms.Application.X"获取启动路径、app数据路径等的WPF是什么?

C# Array.Contains() 编译错误

javascript - Javascript 更改文本后获取 TextBox 文本

c# - 在 ASP.Net MVC Core 2.0 中设置登录 url

javascript - 我的标记中的引号替换为 html 实体?

java - Null Check 增加圈复杂度