我们正在将我们的代码从 c++ 迁移到 c#,我需要我们的一种格式的帮助。
在 c++ 中,我们有下面的例子(假设 myString 类有一个 Format 函数):
myString.Format("%02.0Lf", (long double)amount)
c# 中的等效格式是什么?是吗 :
myString = String::Format("{0:D2}", (long double)amount);
谢谢大家
c# has String class that contains a lot of functions for handling strings.
You could do what you want with String.Format(...)
myString = String.Format("{0:D2}", (long double)amount);
This page will help you http://msdn.microsoft.com/en-us/library/system.string.format.aspx
And here you can find the format strings: http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
double amount = 12.3456;
string text = amount.ToString("0.00");
You're close:
String.Format("{0:D2}", amount)
If this is for an object/class. Consider overriding the class's ToString()
method which is inherited for every class (part of the base Object
class).