简体   繁体   English

String.Format - 从C ++转移到C#

[英]String.Format - moving from C++ to C#

we are upgrading our code from C++ to C# and in alot of place we are formating strings. 我们正在将代码从C ++升级到C#,而且很多地方我们正在编写字符串。 for exmaple we have something like that: 例如,我们有类似的东西:

OurString.Format("amount = %0.2Lf", (long double)amount);

How to convert the %0.2Lf into C# format ? 如何将%0.2Lf转换为C#格式? i've tried the following code but it's not the same 我尝试了下面的代码,但它不一样

string formatString = String.Format("amount = {0}", (long double)amount));

thx 谢谢

Use format strings: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx 使用格式字符串: http//msdn.microsoft.com/en-us/library/dwhawy9k.aspx

You probably want either N2 or F2 , depending on whether to include the thousands separator. 您可能需要N2F2 ,具体取决于是否包含千位分隔符。 So, 所以,

string formatString = String.Format("amount = {0:F2}", amount);

Edit: 编辑:

As an simple FYI, if you're constructing a string in a series of distinct steps, you might want to use the StringBuilder class, which has an AppendFormat() method that accepts the same formating options as string.Format() : 作为一个简单的FYI,如果你在一系列不同的步骤中构造一个字符串,你可能想要使用StringBuilder类,它有一个AppendFormat()方法,它接受与string.Format()相同的格式化选项:

var builder = new StringBuilder();

// ... Your code to build the first part of the string

builder.AppendFormat("amount = {0:F2}", amount);

// ... whatever else you need to add

builder.ToString(); // outputs your final/completed string.

Whether you use StringBuilder or not depends on how critical memory management and/or performance is to the application; 是否使用StringBuilder取决于应用程序对内存管理和/或性能的重要程度; strings in C# are immutable, so when you concat them you are actually creating a brand new string. C#中的字符串是不可变的,所以当你连接它们时,你实际上是在创建一个全新的字符串。 You can read more here: http://msdn.microsoft.com/en-us/library/system.string.aspx#Immutability 你可以在这里阅读更多内容: http//msdn.microsoft.com/en-us/library/system.string.aspx#Immutability

As Tieson wrote, F2 ... Just remember that in C the default locale is the C locale. 正如Tieson所写, F2 ......请记住,在C语言中,默认语言环境是C语言环境。 So for example 0.00 , while in C# the default locale is the current locale of the user (here in Italy 0,00 ). 所以例如0.00 ,而在C#中,默认语言环境是用户的当前语言环境(这里是意大利语0,00 )。 So it would be better to do: 所以最好这样做:

string formatString = String.Format(CultureInfo.InvariantLocale, 
                                    "amount = {0:F2}", amount);

if you want to always everywhere format your digits in the same manner. 如果您希望始终以相同的方式格式化您的数字。 Remember that when I told "here in Italy" also means "if I take my Italian Windows 8 in the USA the default locale will be Italian for me." 请记住,当我告诉“在意大利这里”也意味着“如果我在美国使用我的意大利Windows 8,我的默认语言环境将是意大利语。”

Ah... and there aren't long double s in C# (or in .NET, or in newer versions of VC++ for that reason). 啊......并且在C#中没有long double (或者在.NET中,或者在VC ++的新版本中出于这个原因)。 Only double s. 只有double

I'll add that using a double (or a long double ) for money is an antipattern in itself... There is the decimal type for it. 我要补充一点,使用double (或long double )换钱本身就是反模式......它有十进制类型。

The Decimal value type represents decimal numbers ranging from positive 79,228,162,514,264,337,593,543,950,335 to negative 79,228,162,514,264,337,593,543,950,335. 十进制值类型表示十进制数,范围从正数79,228,162,514,264,337,593,543,950,335到负数79,228,162,514,264,337,593,543,950,335。 The Decimal value type is appropriate for financial calculations that require large numbers of significant integral and fractional digits and no round-off errors. Decimal值类型适用于需要大量有效积分和小数位且没有舍入误差的财务计算。 The Decimal type does not eliminate the need for rounding. Decimal类型不会消除舍入的需要。 Rather, it minimizes errors due to rounding. 相反,它最大限度地减少了因舍入而导致的错误。 For example, the following code produces a result of 0.9999999999999999999999999999 instead of 1. 例如,以下代码生成的结果为0.9999999999999999999999999999而不是1。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM