0

我们正在将我们的代码从 c++ 迁移到 c#,我需要我们的一种格式的帮助。

在 c++ 中,我们有下面的例子(假设 myString 类有一个 Format 函数):

myString.Format("%02.0Lf", (long double)amount)

c# 中的等效格式是什么?是吗 :

myString = String::Format("{0:D2}", (long double)amount);

谢谢大家

4

3 回答 3

0

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

于 2013-09-23T05:08:11.743 回答
0
double amount = 12.3456;
string text = amount.ToString("0.00");
于 2013-09-23T05:08:44.477 回答
0

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).

于 2013-09-23T05:10:28.577 回答