ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • Formating strings
    Search: C#(Csharp) C#(Csharp) 2021. 7. 27. 08:52

     

    string r = "Percent: " + $"{12.3456:F2}" + "%";
    //Percent: 12.34%
    decimal value = 123.4567m;
    Console.WriteLine(value.ToString("C2"));
    //$123.46
    double value = 12345.6789;
    Console.WriteLine(value.ToString("E10", CultureInfo.InvariantCulture));
    //1.2345678900E+004
    Console.WriteLine(value.ToString("E",
                      CultureInfo.CreateSpecificCulture("fr-FR")));
    //1,234568E+004
    double dbv = -12345.6789;
    Console.WriteLine(dbv.ToString("N", CultureInfo.InvariantCulture));
    // -12,345.68
    double number = .2345078;
    Console.WriteLine(number.ToString("P", CultureInfo.InvariantCulture));
    //23.45 %
    CultureInfo ci = new CultureInfo("en-us");
    int iv = 255;
    Console.WriteLine("X: 0x{0}",
            iv.ToString("X", ci));
    //"X: 0xFF"

     

     

    Currency

    123.456 ("C", en-US)
    -> $123.46

    123.456 ("C", fr-FR)
    -> 123,46 €

    123.456 ("C", ja-JP)
    -> ¥123

    -123.456 ("C3", en-US)
    -> ($123.456)

    -123.456 ("C3", fr-FR)
    -> -123,456 €

    -123.456 ("C3", ja-JP)
    -> -¥123.456

     

    Decimal

    1234 ("D")
    -> 1234

    -1234 ("D6")
    -> -001234

     

    Exponential (scientific)

    1052.0329112756 ("E", en-US)
    -> 1.052033E+003

    1052.0329112756 ("e", fr-FR)
    -> 1,052033e+003

    -1052.0329112756 ("e2", en-US)
    -> -1.05e+003

    -1052.0329112756 ("E2", fr-FR)
    -> -1,05E+003

     

    Fixed-point

    1234.567 ("F", en-US)
    -> 1234.57

    1234.567 ("F", de-DE)
    -> 1234,57

    1234 ("F1", en-US)
    -> 1234.0

    1234 ("F1", de-DE)
    -> 1234,0

    -1234.56 ("F4", en-US)
    -> -1234.5600

    -1234.56 ("F4", de-DE)
    -> -1234,5600

     

    General

    -123.456 ("G", en-US)
    -> -123.456

    -123.456 ("G", sv-SE)
    -> -123,456

    123.4546 ("G4", en-US)
    -> 123.5

    123.4546 ("G4", sv-SE)
    -> 123,5

    -1.234567890e-25 ("G", en-US)
    -> -1.23456789E-25

    -1.234567890e-25 ("G", sv-SE)

     

    https://docs.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings

     

    'C#(Csharp)' 카테고리의 다른 글

    C# List  (0) 2023.02.15
    SortedList  (0) 2021.08.08
    Struct and Class  (0) 2021.07.31
    실수형 소수점 자리 제한하여 출력(float, decimal)  (0) 2021.07.27
    List.Sort  (0) 2021.07.26
    SortedSet  (0) 2021.07.24
    HashSet  (0) 2021.07.24
    Csc Ex  (0) 2021.06.23

    댓글