user entering decimal value but view purpose converting to $value
ex:9.86 to $9.86
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
VulpesRank #55 · 29.2K reputationPosted Sep 11, 2013, 4:23 AM EDT
decimal dec = 12.34m;
string dollar = "$" + dec.ToString("F"); // output to 2 dp
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Sep 11, 2013, 4:19 AM EDT
use this name space:
using System.Globalization;
in aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
in.cs:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Globalization;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
Decimal currency = Convert.ToDecimal(TextBox1.Text, new CultureInfo("en-US"));
string money = currency.ToString("C");
if (currency % 1 == 0)
{
money = money.Substring(0, money.Length - 3);
}
Label1.Text = money.ToString();
}
}
Thrish53 reputationPosted Sep 11, 2013, 7:17 AM EDT
{
decimal dollarAmount = decimal.Parse(textBox2.Text);
string text = dollarAmount.ToString("C");
textBox2.Text = text;
}