Advertisement
Guest User

Форматированный вывод в D

a guest
May 7th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 2.87 KB | None | 0 0
  1.  
  2.     items ~= 1.23;
  3.     items ~= 45.6;
  4.  
  5.     for (int i = 0; i != items.length; ++i) {
  6.         writeln("Item ", i + 1, ": ", items[i]);
  7.     }
  8.  
  9.  
  10.     writefln("Item %d:%9.02f", i + 1, items[i]);
  11.  
  12.  
  13.     writefln("Binary     : %b", value);
  14.     writefln("Octal      : %o", value);
  15.     writefln("Hexadecimal: %x", value);
  16.     writefln("Decimal    : %d", value);
  17.  
  18.  
  19.     double value = 123.456789;
  20.  
  21.     writefln("with e: %e", value);
  22.     writefln("with f: %f", value);
  23.     writefln("with g: %g", value);
  24.     writefln("with a: %a", value);
  25.  
  26.  
  27.     bool b = true;
  28.     int i = 365;
  29.     double d = 9.87;
  30.     string s = "formatted";
  31.     auto o = File("test_file", "r");
  32.     int[] a = [ 2, 4, 6, 8 ];
  33.  
  34.     writefln("bool  : %s", b);
  35.     writefln("int   : %s", i);
  36.     writefln("double: %s", d);
  37.     writefln("string: %s", s);
  38.     writefln("object: %s", o);
  39.     writefln("array : %s", a);
  40.  
  41.  
  42.     int value = 100;
  43.  
  44.     writefln("In a field of 10 characters:%10s", value);
  45.     writefln("In a field of 5 characters :%5s", value);
  46.  
  47.  
  48.     double value = 1234.56789;
  49.  
  50.     writefln("%.8g", value);
  51.     writefln("%.3g", value);
  52.     writefln("%.8f", value);
  53.     writefln("%.3f", value);
  54.  
  55.  
  56.     auto number = 0.123456789;
  57.     writefln("Number: %.*g", 4, number);
  58.  
  59.  
  60.     int value = 123;
  61.  
  62.     writefln("Normally right-aligned:|%10d|", value);
  63.     writefln("Left-aligned:|%-10d|", value);
  64.  
  65.  
  66.     writefln("No effect for negative values    : %+d", -50);
  67.     writefln("Positive value with the + flag   : %+d", 50);
  68.     writefln("Positive value without the + flag: %d", 50);
  69.  
  70.  
  71.  
  72.     writefln("Octal starts with 0               : %#o", 1000);
  73.     writefln("Hexadecimal starts with 0x        : %#x", 1000);
  74.     writefln("Contains dot even when unnecessary: %#g", 1f);
  75.     writefln("Rightmost zeros are printed       : %#g", 1.2);
  76.  
  77.  
  78.  
  79.     writefln("In a field of 8 characters: %08d", 42);
  80.  
  81.  
  82.  
  83.     writefln("No effect for negative values: % d", -34);
  84.     writefln("Positive value with space    : % d", 56);
  85.     writefln("Positive value without space : %d", 56);
  86.  
  87.  
  88.     writefln("%1$d %1$x %1$o %1$b", 42);
  89.  
  90.  
  91.     writefln("There are %s students in room %s.", count, room);
  92.  
  93.  
  94.     auto format = (language == "en"
  95.                        ? "There are %s students in room %s."
  96.                        : "%s студентов присутствует в аудитории %s.");
  97.  
  98.     writefln(format, count, room);
  99.  
  100.  
  101.  
  102.     auto format = (language == "en"
  103.                        ? "There are %1$s students in room %2$s."
  104.                        : "В аудитории %2$s присутствует %1$s студентов.");
  105.  
  106.     writefln(format, count, room);
  107.  
  108.  
  109.  
  110.     import std.stdio;
  111.     import std.string;
  112.  
  113.     void main()
  114.     {
  115.         write("Как вас зовут? ");
  116.         auto name = chomp(readln());
  117.         auto result = format("Привет %s!", name);
  118.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement