Printf


printf("%c") .... 

16進数をASCII表示
System::Text::Encoding^ ascii = System::Text::Encoding::ASCII;
array<Byte,1> arr = gcnew ,,,,
Console::Write(ascii->GetString(arr));

Static でもOK

Console::Write(System::Text::Encoding::ASCII->GetString(arr));



フォーマットの{} のエスケープは {{}}

{0:D}の値を中括弧で囲むには 注意 {{{0:D}}}はNG
Console::WriteLine( "\n--- {{{0:D}{1} ---\n",10,"}");

ラウンドトリップ指定子は、数値の変換後の文字列が、変換前の数値へ戻るように解析されることを指定します。

{index[,alignment][:formatString]}


Index コンポーネント

Alignment コンポーネント

Format String コンポーネント

 


cat printf_test.cpp
// printf_test.cpp
// console application

#using <mscorlib.dll>
#using <System.dll>

using namespace System;

#include "printf_test.h"
enum class Color {Yellow = 1, Blue, Green};

int main(array<System::String ^> ^args){
	DateTime thisDate = DateTime::Now;
    Console::Clear();

    // Format a negative integer or floating-point number in various ways.
    Console::WriteLine("Standard Numeric Format Specifiers");
    Console::WriteLine(
        "{{0:C}} Currency: . . . . . . . . {0:C}\n" +
        "{{0:D}} Decimal:. . . . . . . . . {0:D}\n" +
        "{{0:D8}} :                . . . . {0:D8}\n" +
        "{{0:E}} Scientific: . . . . . . . {1:E}\n" +
        "{{0:E9}} :          . . . . . . . {1:E9}\n" +
        "{{0:e2}} :          . . . . . . . {1:e2}\n" +
        "{{0:F}} Fixed point:. . . . . . . {1:F}\n" +
        "{{0:F0}} :          . . . . . . . {1:F0}\n" +
        "{{0:F6}} :          . . . . . . . {1:F6}\n" +
        "{{0:G}} General:. . . . . . . . . {0:G}\n" +
        "    (default):. . . . . . . . {0} (default = 'G')\n" +
        "{{1:G}}        :. . . . . . . . . {1:G}\n" +
        "{{1:G2}}       :. . . . . . . . . {1:G2}\n" +
        "{{2:G}}        :. . . . . . . . . {2:G}\n" +
        "{{2:G2}}       :. . . . . . . . . {2:G2}\n" +
        "{{3:G}}        :. . . . . . . . . {3:G}\n" +
        "{{3:G7}}       :. . . . . . . . . {3:G7}\n" +
        "{{4:N}} Number: . . . . . . . . . {4:N}\n" +
        "{{4:N0}}       : . . . . . . . .  {4:N0}\n" +
        "{{2:P}} Percent:. . . . . . . . . {2:P}\n" +
        "{{2:P4}}       :. . . . . . . . . {2:P4}\n" +
        "{{3:R}} Round-trip: . . . . . . . {3:R}\n" +
        "{{0:X}} Hexadecimal:. . . . . . . {0:X}\n" +
        "{{0:x}}            :. . . . . . . {0:x}\n" +
        "{{4:X8}}           :. . . . . . . {4:X8}\n",
        -123, -123.45f, 0.01234567,Math::PI,1234567890); 
	//
    // Format with placeholders
    Console::WriteLine("Placeholder");
    Console::WriteLine(
        "{{3:0###.##}}      : . . . . . . . {3:0###.##}\n" +
        "{{3:000.###}}      : . . . . . . . {3:000.###}\n" +
        "{{3:%000.##}}      : . . . . . . . {3:%000.##}\n" +
        "{{3:000.##E-00}}   : . . . . . . . {3:000.##E-00}\n" +
        "{{3:000.##E+00}}   : . . . . . . . {3:000.##E+00}\n" +
        "{{3:000.##e+000}}  : . . . . . . . {3:000.##e+000}\n" +
        "{{3:000.##e-000}}  : . . . . . . . {3:000.##e-0000}\n",
        -123, -123.45f, 0.01234567,Math::PI,1234567890); 

	Console::WriteLine("\nSection separator");
	String^ fmt= gcnew String("{0:Positive Number = #.##;Negative Number = -#.##;Zero Value = #.#}\n");
	Console::WriteLine( " using format\n"+fmt);
	Console::WriteLine( fmt, Math::PI);
	Console::WriteLine( fmt, -Math::PI);
	Console::WriteLine( fmt, 0.0);

	Console::WriteLine( "\nescape test to have parenthesis around {0:D} \n");
	Console::WriteLine( "--- {{{0:D}{1} ---\n",10,"}");

	// +-0
	Console::WriteLine( "{{$#,##0.00;($#,##0.00);Zero}} ... {0:$#,##0.00;($#,##0.00);Zero}",Math::PI);
	Console::WriteLine( "{{$#,##0.00;($#,##0.00);Zero}} ... {0:$#,##0.00;($#,##0.00);Zero}",-Math::PI);
	Console::WriteLine( "{{$#,##0.00;($#,##0.00);Zero}} ... {0:$#,##0.00;($#,##0.00);Zero}",0.0);

    // Format phone
    Console::WriteLine("\nphone number");
	Double telnum=1234567890;
	String^ phone= gcnew String( telnum.ToString("(###)###-####"));
    Console::WriteLine("{0} {{0:(###)###-####}} {0:(###)####-####}",telnum,phone);
    Console::WriteLine("From Double.String .........{1}",telnum,phone);


    // Format the current date in various ways.
    Console::WriteLine("\nStandard DateTime Format Specifiers");
	String^ result = String::Format(System::Globalization::CultureInfo::CurrentCulture,
	        
        "{{0:d}} Short date: . . . . . . . {0:d}\n" +
        "{{0:D}} Long date:. . . . . . . . {0:D}\n" +
        "{{0:t}} Short time: . . . . . . . {0:t}\n" +
        "{{0:T}} Long time:. . . . . . . . {0:T}\n" +
        "{{0:f}} Full date/short time: . . {0:f}\n" +
        "{{0:F}} Full date/long time:. . . {0:F}\n" +
        "{{0:g}} General date/short time:. {0:g}\n" +
        "{{0:G}} General date/long time: . {0:G}\n" +
        "    (default):. . . . . . . . {0} (default = 'G')\n" +
        "{{0:M}} Month:. . . . . . . . . . {0:M}\n" +
        "{{0:R}} RFC1123:. . . . . . . . . {0:R}\n" +
        "{{0:s}} Sortable: . . . . . . . . {0:s}\n" +
        "{{0:u}} Universal sortable: . . . {0:u} (invariant)\n" +
        "{{0:U}} Universal sortable: . . . {0:U}\n" +
        "{{0:Y}} Year: . . . . . . . . . . {0:Y}\n" +
        "{{0:dddd MMMM}} : . . . . . . . . {0:dddd MMMM}\n" +
        "{{0:ddd MMM}}   : . . . . . . . . {0:ddd MMM}\n" +
        "{{0:ddd MMM}}   : . . . . . . . . {0:ddd MMM}\n" +
        "{{0:dd}}        : . . . . . . . . {0:dd}\n" +
        "{{0:hh mm}}     : . . . . . . . . {0:hh mm}\n" +
        "{{0:hh}}        : . . . . . . . . {0:hh }\n" +
        "{{0:HH}}        : . . . . . . . . {0:HH }\n" +
        "{{0:MM}}        : . . . . . . . . {0:MM }\n" +
        "{{0:mm}}        : . . . . . . . . {0:mm }\n" +
        "{{0:Y}} Year: . . . . . . . . . . {0:Y}\n",

        thisDate);
    Console::WriteLine( result);


    // Format a Color enumeration value in various ways.
    Console::WriteLine("Standard Enumeration Format Specifiers");
    Console::WriteLine(
        "{{0:G}} General:. . . . . . . . . {0:G}\n" +
        "    (default):. . . . . . . . {0} (default = 'G')\n" +
        "{{0:F}} Flags:. . . . . . . . . . {0:F} (flags or integer)\n" +
        "{{0:D}} Decimal number: . . . . . {0:D}\n" +
        "{{0:X}} Hexadecimal:. . . . . . . {0:X}\n", 
        Color::Green);   


	// Format left and Right alignment
    Console::WriteLine("Alignment\n");
    Console::WriteLine(
		"{{0,-12:G2}} LEFT ..................{0,-12:G2}{0,-12:G2}{0,-12:G2}\n" +
		"{{0,12:G2}} RIGHT ..................{0,12:G2}{0,12:G2}{0,12:G2}\n" ,
	Math::PI, "MSG");


	// sprintf
    Console::WriteLine("\nsprintf\n");
	String^ s = String::Format("---{0,12:G2}---\n",Math::PI);
    Console::WriteLine(s);

	// Switch the CultureInfo
	
	System::Globalization::CultureInfo^ MyCI = gcnew System::Globalization::CultureInfo( "en-US" );
	//System::Globalization::CultureInfo^ MyCI = gcnew System::Globalization::CultureInfo( "en-US",false );
	System::Globalization::DateTimeFormatInfo^ myDTFI = MyCI->DateTimeFormat;

	// Creates a DateTime with the Gregorian date January 3, 2002 (year=2002, month=1, day=3).
	// The Gregorian calendar is the default calendar for the en-US culture.
	DateTime myDT = DateTime(2002,1,3);
//	DateTime myDT = DateTime(thisDate);

	//Display the native calendar name.    
	Console::WriteLine("\nNativeCalendarName...");
	Console::WriteLine("\"{0}\"", myDTFI->NativeCalendarName);

	// Display month genitive names.
	Console::WriteLine("\nMonthGenitiveNames...");
	for each (String^% name in myDTFI->MonthGenitiveNames) 
	{
		Console::WriteLine("\"{0}\"", name);
	}
	Console::WriteLine("\nAbbreviatedMonthGenitiveNames...");
	for each (String^% name in myDTFI->AbbreviatedMonthGenitiveNames) 
	{
		Console::WriteLine("\"{0}\"", name);
	}
	Console::WriteLine("\nShortestDayNames...");
	for each (String^% name in myDTFI->ShortestDayNames) 
	{
		Console::WriteLine("\"{0}\"", name);
	}
//	Display shortest day name for a particular day of the week.
	Console::WriteLine("\nGetShortestDayName(DayOfWeek.Sunday)...");
	Console::WriteLine("\"{0}\"", myDTFI->GetShortestDayName(DayOfWeek::Sunday));
	//Display the initial DateTime format patterns for the 'd' format specifier.
	Console::WriteLine("\nInitial DateTime format patterns for the 'd' format specifier...");
	for each (String^% name in myDTFI->GetAllDateTimePatterns('d')) 
	{
		Console::WriteLine("\"{0}\"", name);
	}
	//Change the initial DateTime format patterns for the 'd' DateTime format specifier.
	Console::WriteLine("\nChange the initial DateTime format patterns for the \n" + "'d' format specifier to my format patterns...");
	array<String^>^ myDateTimePatterns = gcnew array<String^>{"MM/dd/yy", "MM/dd/yyyy"};

	myDTFI->SetAllDateTimePatterns(myDateTimePatterns, 'd');
	 Console::WriteLine("\nNew DateTime format patterns for the 'd' format specifier...");

	for each (String^% name in myDTFI->GetAllDateTimePatterns('d')) 
	{
		Console::WriteLine("\"{0}\"", name);
	}




	// Displays the format pattern associated with each format character.
	Console::WriteLine( "FORMAT  {0}",MyCI->Name );
	Console::WriteLine( "CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY\n" );
	Console::WriteLine( "  d     {0}", myDT.ToString( "d",MyCI ) );
	Console::WriteLine( "        {0} {1}\n", myDTFI->ShortDatePattern, "(ShortDatePattern)" );
	Console::WriteLine( "  D     {0}", myDT.ToString( "D",MyCI ) );
	Console::WriteLine( "        {0} {1}\n", myDTFI->LongDatePattern, "(LongDatePattern)" );
	Console::WriteLine( "  f     {0}\n", myDT.ToString( "f",MyCI ) );
	Console::WriteLine( "  F     {0}", myDT.ToString( "F",MyCI ) );
	Console::WriteLine( "        {0} {1}\n", myDTFI->FullDateTimePattern, "(FullDateTimePattern)" );
	Console::WriteLine( "  g     {0}\n", myDT.ToString( "g",MyCI ) );
	Console::WriteLine( "  G     {0}\n", myDT.ToString( "G",MyCI ) );
	Console::WriteLine( "  m     {0}", myDT.ToString( "m",MyCI ) );
	Console::WriteLine( "        {0} {1}\n", myDTFI->MonthDayPattern, "(MonthDayPattern)" );
	Console::WriteLine( "  M     {0}", myDT.ToString( "M",MyCI ) );
	Console::WriteLine( "        {0} {1}\n", myDTFI->MonthDayPattern, "(MonthDayPattern)" );
	Console::WriteLine( "  o     {0}\n", myDT.ToString("o",MyCI) );
	Console::WriteLine( "  r     {0}", myDT.ToString( "r",MyCI ) );
	Console::WriteLine( "        {0} {1}\n", myDTFI->RFC1123Pattern, "(RFC1123Pattern)" );
	Console::WriteLine( "  R     {0}", myDT.ToString( "R",MyCI ) );
	Console::WriteLine( "        {0} {1}\n", myDTFI->RFC1123Pattern, "(RFC1123Pattern)" );
	Console::WriteLine( "  s     {0}", myDT.ToString( "s",MyCI ) );
	Console::WriteLine( "        {0} {1}\n", myDTFI->SortableDateTimePattern, "(SortableDateTimePattern)" );
	Console::WriteLine( "  t     {0}", myDT.ToString( "t",MyCI ) );
	Console::WriteLine( "        {0} {1}\n", myDTFI->ShortTimePattern, "(ShortTimePattern)" );
	Console::WriteLine( "  T     {0}", myDT.ToString( "T",MyCI ) );
	Console::WriteLine( "        {0} {1}\n", myDTFI->LongTimePattern, "(LongTimePattern)" );
	Console::WriteLine( "  u     {0}", myDT.ToString( "u",MyCI ) );
	Console::WriteLine( "        {0} {1}\n", myDTFI->UniversalSortableDateTimePattern, "(UniversalSortableDateTimePattern)" );
	Console::WriteLine( "  U     {0}\n", myDT.ToString( "U",MyCI ) );
	Console::WriteLine( "  y     {0}", myDT.ToString( "y",MyCI ) );
	Console::WriteLine( "        {0} {1}\n", myDTFI->YearMonthPattern, "(YearMonthPattern)" );
	Console::WriteLine( "  Y     {0}", myDT.ToString( "Y",MyCI ) );
	Console::WriteLine( "        {0} {1}\n", myDTFI->YearMonthPattern, "(YearMonthPattern)" );

	return EXIT_SUCCESS;
}


cat makefile
# makefile for printf_test
# console application

.PHONY: all test mmm clean 
.SUFFIXES: .exe .obj .txt .resources .dll

TARGET=printf_test

CULTUREDIRS=
DLLS=

all: $(TARGET).exe $(DLLS)


LOPT=

OBJS=$(TARGET).obj AssemblyInfo.obj

$(TARGET).obj: $(TARGET).cpp


AssemblyInfo.obj: AssemblyInfo.cpp

meta: $(TARGET).exe
	ILDasm /out:$@ /metadata $<

$(TARGET).exe: $(OBJS)  
	link $^ /out:$@ $(LOPT)

.cpp.obj:
	cl -c -Wall -Od -clr:safe $<

test: $(TARGET).exe
	./$(TARGET).exe 

clean:
	@-rm $(TARGET).exe *.obj *~ $(TARGET).exe.manifest 1>/dev/null 2>&1
	@-rm *.resources 1>/dev/null 2>&1
	@-rm -fr $(CULTUREDIRS) 1>/dev/null 2>&1
	@-rm meta 1>/dev/null 2>&1

mmm:
	cat $(TARGET).cpp
	cat AssemblyInfo.cpp
	@echo
	cat makefile

html:
	cat /dev/clipboard | code2html.exe | unix2dos

install: $(TARGET).exe
	cp $(TARGET).exe ~/bin
	cp $(TARGET).exe.manifest ~/bin


link printf_test.obj AssemblyInfo.obj /out:printf_test.exe 
Microsoft (R) Incremental Linker Version 8.00.50727.42

Copyright (C) Microsoft Corporation.  All rights reserved.



./printf_test.exe 
Standard Numeric Format Specifiers

{0:C} Currency: . . . . . . . . -\123
{0:D} Decimal:. . . . . . . . . -123
{0:D8} :                . . . . -00000123
{0:E} Scientific: . . . . . . . -1.234500E+002
{0:E9} :          . . . . . . . -1.234499970E+002
{0:e2} :          . . . . . . . -1.23e+002
{0:F} Fixed point:. . . . . . . -123.45
{0:F0} :          . . . . . . . -123
{0:F6} :          . . . . . . . -123.450000
{0:G} General:. . . . . . . . . -123
    (default):. . . . . . . . -123 (default = 'G')
{1:G}        :. . . . . . . . . -123.45
{1:G2}       :. . . . . . . . . -1.2E+02
{2:G}        :. . . . . . . . . 0.01234567
{2:G2}       :. . . . . . . . . 0.012
{3:G}        :. . . . . . . . . 3.14159265358979
{3:G7}       :. . . . . . . . . 3.141593
{4:N} Number: . . . . . . . . . 1,234,567,890.00
{4:N0}       : . . . . . . . .  1,234,567,890
{2:P} Percent:. . . . . . . . . 1.23%
{2:P4}       :. . . . . . . . . 1.2346%
{3:R} Round-trip: . . . . . . . 3.1415926535897931
{0:X} Hexadecimal:. . . . . . . FFFFFF85
{0:x}            :. . . . . . . ffffff85
{4:X8}           :. . . . . . . 499602D2


Placeholder

{3:0###.##}      : . . . . . . . 0003.14
{3:000.###}      : . . . . . . . 003.142
{3:%000.##}      : . . . . . . . %314.16
{3:000.##E-00}   : . . . . . . . 314.16E-02
{3:000.##E+00}   : . . . . . . . 314.16E-02
{3:000.##e+000}  : . . . . . . . 314.16e-002
{3:000.##e-000}  : . . . . . . . 314.16e-0002



Section separator

 using format
{0:Positive Number = #.##;Negative Number = -#.##;Zero Value = #.#}


Positive Number = 3.14


Negative Number = -3.14


Zero Value = 



escape test to have parenthesis around {0:D} 


--- {10} ---


{$#,##0.00;($#,##0.00);Zero} ... $3.14

{$#,##0.00;($#,##0.00);Zero} ... ($3.14)

{$#,##0.00;($#,##0.00);Zero} ... Zero


phone number

1234567890 {0:(###)###-####} (12)3456-7890

From Double.String .........(123)456-7890


Standard DateTime Format Specifiers

{0:d} Short date: . . . . . . . 2007/04/07
{0:D} Long date:. . . . . . . . 2007年4月7日
{0:t} Short time: . . . . . . . 1:21
{0:T} Long time:. . . . . . . . 1:21:39
{0:f} Full date/short time: . . 2007年4月7日 1:21
{0:F} Full date/long time:. . . 2007年4月7日 1:21:39
{0:g} General date/short time:. 2007/04/07 1:21
{0:G} General date/long time: . 2007/04/07 1:21:39
    (default):. . . . . . . . 2007/04/07 1:21:39 (default = 'G')
{0:M} Month:. . . . . . . . . . 4月7日
{0:R} RFC1123:. . . . . . . . . Sat, 07 Apr 2007 01:21:39 GMT
{0:s} Sortable: . . . . . . . . 2007-04-07T01:21:39
{0:u} Universal sortable: . . . 2007-04-07 01:21:39Z (invariant)
{0:U} Universal sortable: . . . 2007年4月6日 16:21:39
{0:Y} Year: . . . . . . . . . . 2007年4月
{0:dddd MMMM} : . . . . . . . . 土曜日 4月
{0:ddd MMM}   : . . . . . . . . 土 4
{0:ddd MMM}   : . . . . . . . . 土 4
{0:dd}        : . . . . . . . . 07
{0:hh mm}     : . . . . . . . . 01 21
{0:hh}        : . . . . . . . . 01 
{0:HH}        : . . . . . . . . 01 
{0:MM}        : . . . . . . . . 04 
{0:mm}        : . . . . . . . . 21 
{0:Y} Year: . . . . . . . . . . 2007年4月


Standard Enumeration Format Specifiers

{0:G} General:. . . . . . . . . Green
    (default):. . . . . . . . Green (default = 'G')
{0:F} Flags:. . . . . . . . . . Green (flags or integer)
{0:D} Decimal number: . . . . . 3
{0:X} Hexadecimal:. . . . . . . 00000003


Alignment


{0,-12:G2} LEFT ..................3.1         3.1         3.1         
{0,12:G2} RIGHT ..................         3.1         3.1         3.1



sprintf


---         3.1---



NativeCalendarName...

"Gregorian Calendar"


MonthGenitiveNames...

"January"

"February"

"March"

"April"

"May"

"June"

"July"

"August"

"September"

"October"

"November"

"December"

""


AbbreviatedMonthGenitiveNames...

"Jan"

"Feb"

"Mar"

"Apr"

"May"

"Jun"

"Jul"

"Aug"

"Sep"

"Oct"

"Nov"

"Dec"

""


ShortestDayNames...

"Su"

"Mo"

"Tu"

"We"

"Th"

"Fr"

"Sa"


GetShortestDayName(DayOfWeek.Sunday)...

"Su"


Initial DateTime format patterns for the 'd' format specifier...

"M/d/yyyy"

"M/d/yy"

"MM/dd/yy"

"MM/dd/yyyy"

"yy/MM/dd"

"yyyy-MM-dd"

"dd-MMM-yy"


Change the initial DateTime format patterns for the 
'd' format specifier to my format patterns...


New DateTime format patterns for the 'd' format specifier...

"MM/dd/yy"

"MM/dd/yyyy"

FORMAT  en-US

CHAR    VALUE OF ASSOCIATED PROPERTY, IF ANY


  d     01/03/02

        MM/dd/yy (ShortDatePattern)


  D     Thursday, January 03, 2002

        dddd, MMMM dd, yyyy (LongDatePattern)


  f     Thursday, January 03, 2002 12:00 AM


  F     Thursday, January 03, 2002 12:00:00 AM

        dddd, MMMM dd, yyyy h:mm:ss tt (FullDateTimePattern)


  g     01/03/02 12:00 AM


  G     01/03/02 12:00:00 AM


  m     January 03

        MMMM dd (MonthDayPattern)


  M     January 03

        MMMM dd (MonthDayPattern)


  o     2002-01-03T00:00:00.0000000


  r     Thu, 03 Jan 2002 00:00:00 GMT

        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)


  R     Thu, 03 Jan 2002 00:00:00 GMT

        ddd, dd MMM yyyy HH':'mm':'ss 'GMT' (RFC1123Pattern)


  s     2002-01-03T00:00:00

        yyyy'-'MM'-'dd'T'HH':'mm':'ss (SortableDateTimePattern)


  t     12:00 AM

        h:mm tt (ShortTimePattern)


  T     12:00:00 AM

        h:mm:ss tt (LongTimePattern)


  u     2002-01-03 00:00:00Z

        yyyy'-'MM'-'dd HH':'mm':'ss'Z' (UniversalSortableDateTimePattern)


  U     Wednesday, January 02, 2002 3:00:00 PM


  y     January, 2002

        MMMM, yyyy (YearMonthPattern)


  Y     January, 2002

        MMMM, yyyy (YearMonthPattern)