C# 中的 Convert.ToDecimal 方法


使用 Convert.ToDecimal() 方法,將指定的值轉換為十進位制數字。

這裡有一個字串。

string stringVal = "2,345.26";

現在,讓我們使用 Convert.ToDecimal() 方法將其轉換為十進位制數字。

decimal decimalVal;
decimalVal = System.Convert.ToDecimal(stringVal);

現在讓我們看完整的示例 −

示例

 線上演示

using System;
public class Demo {
   public static void Main() {
      decimal decimalVal;
      string stringVal = "2,345.26";
      decimalVal = System.Convert.ToDecimal(stringVal);
      System.Console.WriteLine("String converted to decimal = {0} ", decimalVal);
   }
}

輸出

String converted to decimal = 2345.26

更新於: 2020 年 6 月 23 日

4K+ 瀏覽量

開啟你的 職業生涯

完成課程,獲得認證

開始
廣告
© . All rights reserved.