CSharp examples for Language Basics:double
Rounding double value
using System;//from w w w . j a v a2s .c o m using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; class Program { static void Main(string[] args) { double amount1 = 1234.567; double amount2 = 9.876; Console.WriteLine("First amount (original value): " + amount1); Console.WriteLine("Second amount (original value): " + amount2); Console.WriteLine("First amount displayed with cent precision: " + amount1.ToString("N2")); Console.WriteLine("Second amount displayed with cent precision: " + amount2.ToString("N2")); double roundedAmount1 = Math.Round(amount1, 2); // 2 = two decimal places double roundedAmount2 = Math.Round(amount2, 2); Console.WriteLine("First amount rounded to cents: " + roundedAmount1); Console.WriteLine("Second amount rounded to cents: " + roundedAmount2); double sumOfOriginalAmounts = amount1 + amount2; double sumOfRoundedAmounts = roundedAmount1 + roundedAmount2; Console.WriteLine("Sum of original amounts: " + sumOfOriginalAmounts.ToString("N2")); Console.WriteLine("Sum of rounded amounts: " + sumOfRoundedAmounts.ToString("N2")); Console.WriteLine("On invoice, we need sum of rounded amounts"); } }