用 C# 中的數字運算子操作小數


使用 C#,你可以使用 _+, - *, 等運算子操作小數。

讓我們看看如何減去小數值。

首先,設定兩個小數值 −

decimal d1 = 9.5M;
decimal d2 = 4.2M;

現在減去這兩個值 −

d1 = d1 - d2;

以下是程式碼 −

示例

 線上演示

using System;
using System.Linq;
class Demo {
   static void Main() {
      decimal d1 = 9.5M;
      decimal d2 = 4.2M;
      d1 = d1 + d2;
      Console.WriteLine("Addition of Decimals: "+d1);
      d1 = d1 - d2;
      Console.WriteLine("Subtraction of Decimals: "+d1);
   }
}

輸出

Addition of Decimals: 13.7
Subtraction of Decimals: 9.5

更新時間:2020 年 6 月 22 日

148 次瀏覽

開啟您的 職業生涯

透過完成課程獲得認證

開始學習
廣告
© . All rights reserved.