|
-
March 9th, 2011, 11:08 PM
#1
[RESOLVED] Why can't I format a decimal into a string?
I need to be able to format a decimal into a string with a specified number of decimal places.
Here is my simple test page code consisting of a a text box, a button and a label. The idea is for the user to enter a decimal number, i.e., 23.345456, then click the button and have the label show the formatted decimal string. But try as I may, I cannot get it to work.
Code:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DecimalFormat.aspx.cs" Inherits="DecimalFormat" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>
<style type="text/css">
.style1
{
font-size: large;
color: #000080;
font-weight: bold;
font-family: Arial, Helvetica, sans-serif;
}
.style2
{
font-family: Arial, Helvetica, sans-serif;
font-weight: bold;
font-size: medium;
color: #000080;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<p class="style1">
TEST DECIMAL FORMAT USING C#</p>
<div>
<span class="style2">Enter Decimal</span> :<asp:TextBox ID="txtBoxDecimal"
runat="server"></asp:TextBox>
<br />
</div>
<asp:Button ID="btnFormat" runat="server" Text="Format"
onclick="btnFormat_Click" />
<asp:Label ID="labelDecimal" runat="server" Font-Bold="True" Font-Names="Arial"
Font-Size="Medium"></asp:Label>
</form>
</body>
</html>
And here is the underlying C# code:
Code:
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Globalization; // NumberFormatInfo class
public partial class DecimalFormat : System.Web.UI.Page
{
public String sDecimal;
public Decimal decnum;
protected void Page_Load(object sender, EventArgs e)
{
NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
nfi.NumberDecimalDigits = 3;
//sDecimal = txtBoxDecimal.ToString();
sDecimal = txtBoxDecimal.Text;
// decnum = txtBoxDecimal.
decnum = Convert.ToDecimal(sDecimal, nfi);
}
protected void btnFormat_Click(object sender, EventArgs e)
{
NumberFormatInfo nfi = new CultureInfo("en-US", false).NumberFormat;
nfi.NumberDecimalDigits = 0;
String sFormatDecimal = Convert.ToString(decnum, nfi);
labelDecimal.Text = sFormatDecimal;
}
}
Appreciate info on how to do this correctly. Thanks.
mpliam
-
March 10th, 2011, 03:38 AM
#2
Re: Why can't I format a decimal into a string?
I'm unsure of what result you want, but the ToString method should be able to do it.
Check out:
http://msdn.microsoft.com/en-us/libr...#DFormatString
and see if it helps.
-
March 10th, 2011, 03:46 PM
#3
Re: Why can't I format a decimal into a string?
Thanks for your response.
I figured out how to format a double value to set the number of decimal digits.
Code:
String sFormatDecimal = value.ToString("F1", CultureInfo.InvariantCulture);
will format the variable double value to a string with one decimal digit. However, I still have not solved the problem.
Code:
static public double value;
static public string sDecimal;
//..
protected void Page_Load(object sender, EventArgs e)
{
sDecimal = txtBoxDecimal.Text;
// value = double.Parse((sDecimal); // Input string was not in a correct format.
}
protected void btnFormat_Click(object sender, EventArgs e)
{
// String sFormatDecimal = value.ToString("F1", CultureInfo.InvariantCulture);
//String sFormatDecimal = Convert.ToString(decnum, nfi);
String sFormatDecimal = sDecimal;
labelDecimal.Text = sFormatDecimal;
}
My problem is that I cannot convert the decimal string in the textbox to a variable double value that can then be formatted. Invariably the error:
'Input string was not in a correct format.' is encountered.
Code:
value = double.Parse(sDecimal);
According to a variety of C# sources on the web, this is supposed to work. I am beginning to wonder if there are exceptions that occur with Asp.NET C# that do not cause a problem in non-Asp.net C# ?
Please help me.
Last edited by Mike Pliam; March 10th, 2011 at 03:48 PM.
mpliam
-
March 14th, 2011, 04:24 AM
#4
Re: Why can't I format a decimal into a string?
C# in asp.net works as C#.
So the problem is with the input.
My guess is that you enter the number in some sort of culture specific value, say using comma as decimal point instead of period, or include some non-value characters.
What exact value do you try to parse? What culture do you parse to and from? I've noticed an en-US in your original code, meaning decimal point is a period. But if you try to parse in, in say culture where period is thousand separator, you'll have issues.
-
March 14th, 2011, 02:42 PM
#5
Re: Why can't I format a decimal into a string?
Alsvha,
Thanks for your suggestion. It led me to examine the input which turned out to be indeed the problem. I had tried on Page_Load to parse an empty textbox which led to the format error. Also, I had left the max length setting of the textbox property to 0. In any case, after I provided a default '0.0' value in the textbox, the following code worked just fine.
Code:
public partial class DecimalFormat : System.Web.UI.Page
{
static public string sDecimal;
static public double value;
protected void Page_Load(object sender, EventArgs e)
{
sDecimal = txtBoxDecimal.Text;
value = double.Parse(sDecimal);
}
protected void btnFormat_Click(object sender, EventArgs e)
{
String sFormatDecimal = value.ToString("F1", CultureInfo.InvariantCulture);
labelDecimal.Text = sFormatDecimal;
}
}
Thankyou for your thoughtful input.
mpliam
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|