當前位置: 首頁>>代碼示例>>C#>>正文


C# Single.ToString方法代碼示例

本文整理匯總了C#中System.Single.ToString方法的典型用法代碼示例。如果您正苦於以下問題:C# Single.ToString方法的具體用法?C# Single.ToString怎麽用?C# Single.ToString使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在System.Single的用法示例。


在下文中一共展示了Single.ToString方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1:

float value = 16325.62901F;
string specifier;
CultureInfo culture;

// Use standard numeric format specifiers.
specifier = "G";
culture = CultureInfo.CreateSpecificCulture("eu-ES");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325,62901
Console.WriteLine(value.ToString(specifier, CultureInfo.InvariantCulture));
// Displays:    16325.62901

specifier = "C";
culture = CultureInfo.CreateSpecificCulture("en-US");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    $16,325.63
culture = CultureInfo.CreateSpecificCulture("en-GB");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    £16,325.63

specifier = "E04";
culture = CultureInfo.CreateSpecificCulture("sv-SE");
Console.WriteLine(value.ToString(specifier, culture));
// Displays: 1,6326E+004   
 culture = CultureInfo.CreateSpecificCulture("en-NZ");
 Console.WriteLine(value.ToString(specifier, culture));
// Displays:    1.6326E+004   

specifier = "F";
culture = CultureInfo.CreateSpecificCulture("fr-FR");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325,63
culture = CultureInfo.CreateSpecificCulture("en-CA");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16325.63

specifier = "N";
culture = CultureInfo.CreateSpecificCulture("es-ES");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16.325,63
culture = CultureInfo.CreateSpecificCulture("fr-CA");
Console.WriteLine(value.ToString(specifier, culture));
// Displays:    16 325,63

specifier = "P";
culture = CultureInfo.InvariantCulture;
Console.WriteLine((value/10000).ToString(specifier, culture));
// Displays:    163.26 %
culture = CultureInfo.CreateSpecificCulture("ar-EG");
Console.WriteLine((value/10000).ToString(specifier, culture));
// Displays:    163.256 %
開發者ID:.NET開發者,項目名稱:System,代碼行數:51,代碼來源:Single.ToString

示例2: Main

//引入命名空間
using System;

public class Example
{
   public static void Main()
   {
      Double number = 1764.3789;
      
      // Format as a currency value.
      Console.WriteLine(number.ToString("C"));
      
      // Format as a numeric value with 3 decimal places.
      Console.WriteLine(number.ToString("N3"));
   }
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:16,代碼來源:Single.ToString

輸出:

$1,764.38
1,764.379

示例3: foreach

float[] numbers= { 1054.32179F, -195489100.8377F, 1.0437E21F, 
                   -1.0573e-05F };
string[] specifiers = { "C", "E", "e", "F", "G", "N", "P", 
                        "R", "#,000.000", "0.###E-000",
                        "000,000,000,000.00###" };

foreach (float number in numbers)
{
   Console.WriteLine("Formatting of {0}:", number);
   foreach (string specifier in specifiers)
      Console.WriteLine("   {0,5}: {1}", 
                        specifier, number.ToString(specifier));

   Console.WriteLine();
}
開發者ID:.NET開發者,項目名稱:System,代碼行數:15,代碼來源:Single.ToString

輸出:

Formatting of 1054.32179:
C: $1,054.32
E: 1.054322E+003
e: 1.054322e+003
F: 1054.32
G: 1054.32179
N: 1,054.32
P: 105,432.18 %
R: 1054.32179
#,000.000: 1,054.322
0.###E-000: 1.054E003
000,000,000,000.00###: 000,000,001,054.322

Formatting of -195489100.8377:
C: ($195,489,100.84)
E: -1.954891E+008
e: -1.954891e+008
F: -195489100.84
G: -195489100.8377
N: -195,489,100.84
P: -19,548,910,083.77 %
R: -195489100.8377
#,000.000: -195,489,100.838
0.###E-000: -1.955E008
000,000,000,000.00###: -000,195,489,100.00

Formatting of 1.0437E+21:
C: $1,043,700,000,000,000,000,000.00
E: 1.043700E+021
e: 1.043700e+021
F: 1043700000000000000000.00
G: 1.0437E+21
N: 1,043,700,000,000,000,000,000.00
P: 104,370,000,000,000,000,000,000.00 %
R: 1.0437E+21
#,000.000: 1,043,700,000,000,000,000,000.000
0.###E-000: 1.044E021
000,000,000,000.00###: 1,043,700,000,000,000,000,000.00

Formatting of -1.0573E-05:
C: $0.00
E: -1.057300E-005
e: -1.057300e-005
F: 0.00
G: -1.0573E-05
N: 0.00
P: 0.00 %
R: -1.0573E-05
#,000.000: 000.000
0.###E-000: -1.057E-005
000,000,000,000.00###: -000,000,000,000.00001

示例4:

float value;

value = -16325.62015F;
// Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
// Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")));
// Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")));

value = 16034.125E21F;
// Display value using the invariant culture.
Console.WriteLine(value.ToString(CultureInfo.InvariantCulture));
// Display value using the en-GB culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("en-GB")));
// Display value using the de-DE culture.
Console.WriteLine(value.ToString(CultureInfo.CreateSpecificCulture("de-DE")));
// This example displays the following output to the console:
//       -16325.62015
//       -16325.62015
//       -16325,62015
//       1.6034125E+25
//       1.6034125E+25
//       1,6034125E+25
開發者ID:.NET開發者,項目名稱:System,代碼行數:24,代碼來源:Single.ToString

示例5:

float number;

number = 1.6E20F;
// Displays 1.6E+20.
Console.WriteLine(number.ToString());

number = 1.6E2F;
// Displays 160.
Console.WriteLine(number.ToString());

number = -3.541F;
// Displays -3.541.
Console.WriteLine(number.ToString());

number = -1502345222199E-07F;
// Displays -150234.5222199.
Console.WriteLine(number.ToString());

number = -15023452221990199574E-09F;
// Displays -15023452221.9902.
Console.WriteLine(number.ToString());

number = .60344F;
// Displays 0.60344.
Console.WriteLine(number.ToString());

number = .000000001F;
// Displays 1E-09.
Console.WriteLine(number.ToString());
開發者ID:.NET開發者,項目名稱:System,代碼行數:29,代碼來源:Single.ToString

示例6: catch

bool done = false;
string inp;
do
{
    Console.Write("Enter a real number: ");
    inp = Console.ReadLine();
    try
    {
        s = Single.Parse(inp);
        Console.WriteLine("You entered {0}.", s.ToString());
        done = true;
    }
    catch (FormatException)
    {
        Console.WriteLine("You did not enter a number.");
    }
    catch (Exception e)
    {
        Console.WriteLine("An exception occurred while parsing your response: {0}", e.ToString());
    }
} while (!done);
開發者ID:.NET開發者,項目名稱:System,代碼行數:21,代碼來源:Single.ToString


注:本文中的System.Single.ToString方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。