c# - How to format a decimal string to currency

string format decimal to currency
the following asp.net c# code example demonstrate us how can we format a string to display decimal value as currency.

decimal keyword indicates a 128-bit data type. decimal type has more precision and a smaller range compared to floating point types. decimal is appropriate for financial and monetary calculations.

String.Format() method replace each format item in a specified string with the text equivalent of corresponding object's value.

in this example, at first we created two decimal type variables with value. one positive value and another one is negative value. we want to display these decimal values in a string as currency. to do that we call the String.Format() method and pass {0:C} as first argument and decimal value as second argument. it format the string to properly display decimal value as currency. we uses standard numeric format specifier C to display currency value.

<%@ Page Language="C#" AutoEventWireup="true"%>

<!DOCTYPE html>  
<script runat="server"> 
    protected void Button1_Click(object sender, System.EventArgs e)  
        //this section create decimal variables.
        Decimal decimalValue1 = 555.55M;
        Decimal decimalValue2 = -777.77M;

        Label1.Text = "decimal value1: " + decimalValue1.ToString();
        Label1.Text += "<br />decimal value2: " + decimalValue2.ToString();

        //string format decimal to currency using string.format method.
        Label1.Text += "<br /><br />format decimal value: " + string.Format("{0:C}", decimalValue1);
        Label1.Text += "<br />format negetive decimal value: " + string.Format("{0:C}", decimalValue2);
<html xmlns="http://www.w3.org/1999/xhtml">  
<head id="Head1" runat="server">  
    <title>c# example - string format decimal to currency</title>  
    <form id="form1" runat="server">  
        <h2 style="color:MidnightBlue; font-style:italic;">  
            c# example - string format decimal to currency
        <hr width="550" align="left" color="Gainsboro" />  
        <br /><br />
            Text="string format decimal to currency"  
More c# examples