ด้วย 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