How to represent currency in C#

1 Answer

0 votes
/*---------------------------------------------------------
  Description:
    Demonstrates how to represent and display currency
    values in C# using decimal and formatting.
---------------------------------------------------------*/

using System;
using System.Globalization;

class CurrencyExample
{
    static void Main()
    {
        // Price and tax rate
        decimal price = 199.99m;
        decimal taxRate = 0.17m; // 17%

        // Calculate tax and total
        decimal taxAmount = price * taxRate;
        decimal total = price + taxAmount;

        // Output with $ and % signs
        Console.WriteLine("Price: " + price.ToString("C", CultureInfo.GetCultureInfo("en-US")));
        Console.WriteLine("Tax Rate: 17%");
        Console.WriteLine("Tax Amount: " + taxAmount.ToString("C", CultureInfo.GetCultureInfo("en-US")));
        Console.WriteLine("Total: " + total.ToString("C", CultureInfo.GetCultureInfo("en-US")));
    }
}



/*
run:

Price: $199.99
Tax Rate: 17%
Tax Amount: $34.00
Total: $233.99

*/

 



answered 3 hours ago by avibootz
...