Visual C++ .NET/Data Type/String Format
Содержание
- 1 { 0,haracters in width, left-justified
- 2 { 1,haracters in width, right-justified
- 3 { 2, 10:c2 } 10 characters in width, currency with 2 decimal places
- 4 String::Format("Currency format: {0:c2}", dbl);
- 5 String::Format("Decimal format: {0:d6}", i);
- 6 String::Format("Fixed-point format: {0:f6}", dbl);
- 7 String::Format("General format: {0:g6}", dbl);
- 8 String::Format("General format: {0:g6}", i);
- 9 String::Format("Hexadecimal format: {0:x8}", i);
- 10 String::Format("Number format: {0:n0}", i);
- 11 String::Format("Number format: {0:n6}", dbl);
- 12 String::Format("Percent format: {0:p6}", dbl);
- 13 String::Format("Round-trip format: {0:r6}", dbl);
- 14 String::Format("Scientific format: {0:e6}", dbl);
{ 0,haracters in width, left-justified
<source lang="csharp">
- include "stdafx.h"
using namespace System; int main() {
String^ format = "{0,-30}{1,10}{2,10:c2}"; String^ header = String::Format(format, "Item", "Quantity", "Price"); String^ str1 = str1->Format(format, "M", 10, 0.99); String^ str2 = str2->Format(format, "G", 1, 12.50); String^ str3 = str3->Format(format, "I", 1, 4.99); Console::WriteLine(header); Console::WriteLine(str1 + "\n" + str2 + "\n" + str3);
}
</source>
{ 1,haracters in width, right-justified
<source lang="csharp">
- include "stdafx.h"
using namespace System; int main() {
String^ format = "{0,-30}{1,10}{2,10:c2}"; String^ header = String::Format(format, "Item", "Quantity", "Price"); String^ str1 = str1->Format(format, "M", 10, 0.99); String^ str2 = str2->Format(format, "G", 1, 12.50); String^ str3 = str3->Format(format, "I", 1, 4.99); Console::WriteLine(header); Console::WriteLine(str1 + "\n" + str2 + "\n" + str3);
}
</source>
{ 2, 10:c2 } 10 characters in width, currency with 2 decimal places
<source lang="csharp">
- include "stdafx.h"
using namespace System; int main() {
String^ format = "{0,-30}{1,10}{2,10:c2}"; String^ header = String::Format(format, "Item", "Quantity", "Price"); String^ str1 = str1->Format(format, "M", 10, 0.99); String^ str2 = str2->Format(format, "G", 1, 12.50); String^ str3 = str3->Format(format, "I", 1, 4.99); Console::WriteLine(header); Console::WriteLine(str1 + "\n" + str2 + "\n" + str3);
}
</source>
String::Format("Currency format: {0:c2}", dbl);
<source lang="csharp">
- include "stdafx.h"
using namespace System; int main() {
String^ str; int i = -73000; double dbl = 1005.01; // Formats for floating-point types: str = String::Format("Currency format: {0:c2}", dbl); Console::WriteLine(str);
}
</source>
String::Format("Decimal format: {0:d6}", i);
<source lang="csharp">
- include "stdafx.h"
using namespace System; int main() {
String^ str; int i = -73000; double dbl = 1005.01;
str = String::Format("Decimal format: {0:d6}", i); Console::WriteLine(str);
}
</source>
String::Format("Fixed-point format: {0:f6}", dbl);
<source lang="csharp">
- include "stdafx.h"
using namespace System; int main() {
String^ str; int i = -73000; double dbl = 1005.01; // Formats for floating-point types: str = String::Format("Fixed-point format: {0:f6}", dbl); Console::WriteLine(str);
}
</source>
String::Format("General format: {0:g6}", dbl);
<source lang="csharp">
- include "stdafx.h"
using namespace System; int main() {
String^ str; int i = -73000; double dbl = 1005.01;
str = String::Format("General format: {0:g6}", dbl); Console::WriteLine(str);
}
</source>
String::Format("General format: {0:g6}", i);
<source lang="csharp">
- include "stdafx.h"
using namespace System; int main() {
String^ str; int i = -73000; double dbl = 1005.01;
str = String::Format("General format: {0:g6}", i); Console::WriteLine(str);
}
</source>
String::Format("Hexadecimal format: {0:x8}", i);
<source lang="csharp">
- include "stdafx.h"
using namespace System; int main() {
String^ str; int i = -73000; double dbl = 1005.01;
str = String::Format("Hexadecimal format: {0:x8}", i); Console::WriteLine(str);
}
</source>
String::Format("Number format: {0:n0}", i);
<source lang="csharp">
- include "stdafx.h"
using namespace System; int main() {
String^ str; int i = -73000; double dbl = 1005.01;
str = String::Format("Number format: {0:n0}", i); Console::WriteLine(str);
}
</source>
String::Format("Number format: {0:n6}", dbl);
<source lang="csharp">
- include "stdafx.h"
using namespace System; int main() {
String^ str; int i = -73000; double dbl = 1005.01;
str = String::Format("Number format: {0:n6}", dbl); Console::WriteLine(str);
}
</source>
String::Format("Percent format: {0:p6}", dbl);
<source lang="csharp">
- include "stdafx.h"
using namespace System; int main() {
String^ str; int i = -73000; double dbl = 1005.01;
str = String::Format("Percent format: {0:p6}", dbl); Console::WriteLine(str);
}
</source>
String::Format("Round-trip format: {0:r6}", dbl);
<source lang="csharp">
- include "stdafx.h"
using namespace System; int main() {
String^ str; int i = -73000; double dbl = 1005.01;
str = String::Format("Round-trip format: {0:r6}", dbl); Console::WriteLine(str);
}
</source>
String::Format("Scientific format: {0:e6}", dbl);
<source lang="csharp">
- include "stdafx.h"
using namespace System; int main() {
String^ str; int i = -73000; double dbl = 1005.01; // Formats for floating-point types: str = String::Format("Scientific format: {0:e6}", dbl); Console::WriteLine(str);
}
</source>