Help with toString( ) numeric formatting

B

BBFrost

I'm receiving decimal values from database queries and placing them on a
report page.

The users want to see the following ....

Db Value Display Value
123.3400 123.34
123.0000 123

i.e. I want to trim trailing zeros and (decimal point if no decimal values
occur). This
ought to be really easy but I'm not having much success. Any help would be
GREATLY
appreciated.

I've played with a number of iterations similar to below but the magic
incantation is still
eluding me.

double iDbValue = (double) mDataRow["db_number"];
string sDbValue = iDbValue.ToString("#.##");

Thanks in advance!

Barry
in Oregon
 
J

Jeffrey Tan[MSFT]

Hi BBFrost,

Based on my understanding, you want to do the custom formatting for your
Numeric value.

I see that you have already found the answer to your question.
ToString("#.##") should work for you.

I think you may need a general specification of the numeric formatting.
Actually, in Numeric Format String, "#" is a digit placeholder, while "."
is a Decimal point.

For more information, please refer to:
"Custom Numeric Format Strings"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconcustomnumericformatstrings.asp

Also, there some samples may help you:
"Custom Numeric Format Strings Output Examples"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconcustomnumericformatstringsoutputexample.asp

=====================================
Please apply my suggestion above and let me know if it meets your need.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
B

BBFrost

Jeffery,

Thanks for the quick reply. It's very much appreciated!

Yup, after clearing up a couple of syntax problems ToString("#.##") did
indeed work.

I wrote a quick test program to play with a few of the more common
iterations I'll need eventually ... I've included the Form1.cs in the
message body because of all the problems with attachments these days. I
hope this atones for my somewhat premature post to the group. :-}

B.T.W. I would love to see more examples of the 'fundamental' use of various
Ms Functions in future Ms documentation. Many of the examples in the
current Ms documentation don't seem to address the most fundamental / basic
use making it very difficult to get 'a quick idea' of how a function or
control can be put quickly into play.

Thanks again ...

Barry
in Oregon

================= Test ToString Numeric formatting =====================

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Data.OleDb;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label8;
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label lblFormattedNbr1_2;
private System.Windows.Forms.Label lblFormattedNbr1_1;
private System.Windows.Forms.Label lblFormattedNbr2_2;
private System.Windows.Forms.Label lblFormattedNbr2_1;
private System.Windows.Forms.Label lblFormattedNbr4_2;
private System.Windows.Forms.Label lblFormattedNbr4_1;
private System.Windows.Forms.Label lblFormattedNbr3_2;
private System.Windows.Forms.Label lblFormattedNbr3_1;
private System.Windows.Forms.Label lblFormattedNbr5_2;
private System.Windows.Forms.Label lblFormattedNbr5_1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//
}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label8 = new System.Windows.Forms.Label();
this.panel1 = new System.Windows.Forms.Panel();
this.lblFormattedNbr5_2 = new System.Windows.Forms.Label();
this.lblFormattedNbr5_1 = new System.Windows.Forms.Label();
this.lblFormattedNbr3_2 = new System.Windows.Forms.Label();
this.lblFormattedNbr3_1 = new System.Windows.Forms.Label();
this.lblFormattedNbr4_2 = new System.Windows.Forms.Label();
this.lblFormattedNbr4_1 = new System.Windows.Forms.Label();
this.lblFormattedNbr2_2 = new System.Windows.Forms.Label();
this.lblFormattedNbr2_1 = new System.Windows.Forms.Label();
this.lblFormattedNbr1_2 = new System.Windows.Forms.Label();
this.lblFormattedNbr1_1 = new System.Windows.Forms.Label();
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.panel1.SuspendLayout();
this.SuspendLayout();
//
// label8
//
this.label8.Location = new System.Drawing.Point(6, 10);
this.label8.Name = "label8";
this.label8.Size = new System.Drawing.Size(592, 23);
this.label8.TabIndex = 22;
this.label8.Text = "Instructions:";
this.label8.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// panel1
//
this.panel1.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.panel1.Controls.Add(this.lblFormattedNbr5_2);
this.panel1.Controls.Add(this.lblFormattedNbr5_1);
this.panel1.Controls.Add(this.lblFormattedNbr3_2);
this.panel1.Controls.Add(this.lblFormattedNbr3_1);
this.panel1.Controls.Add(this.lblFormattedNbr4_2);
this.panel1.Controls.Add(this.lblFormattedNbr4_1);
this.panel1.Controls.Add(this.lblFormattedNbr2_2);
this.panel1.Controls.Add(this.lblFormattedNbr2_1);
this.panel1.Controls.Add(this.lblFormattedNbr1_2);
this.panel1.Controls.Add(this.lblFormattedNbr1_1);
this.panel1.Controls.Add(this.button1);
this.panel1.Controls.Add(this.textBox1);
this.panel1.Controls.Add(this.label1);
this.panel1.Location = new System.Drawing.Point(4, 40);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(594, 338);
this.panel1.TabIndex = 21;
//
// lblFormattedNbr5_2
//
this.lblFormattedNbr5_2.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.lblFormattedNbr5_2.Location = new System.Drawing.Point(224,
186);
this.lblFormattedNbr5_2.Name = "lblFormattedNbr5_2";
this.lblFormattedNbr5_2.Size = new System.Drawing.Size(100, 21);
this.lblFormattedNbr5_2.TabIndex = 12;
this.lblFormattedNbr5_2.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblFormattedNbr5_1
//
this.lblFormattedNbr5_1.Location = new System.Drawing.Point(26,
186);
this.lblFormattedNbr5_1.Name = "lblFormattedNbr5_1";
this.lblFormattedNbr5_1.Size = new System.Drawing.Size(194, 21);
this.lblFormattedNbr5_1.TabIndex = 11;
this.lblFormattedNbr5_1.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// lblFormattedNbr3_2
//
this.lblFormattedNbr3_2.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.lblFormattedNbr3_2.Location = new System.Drawing.Point(224,
134);
this.lblFormattedNbr3_2.Name = "lblFormattedNbr3_2";
this.lblFormattedNbr3_2.Size = new System.Drawing.Size(100, 21);
this.lblFormattedNbr3_2.TabIndex = 10;
this.lblFormattedNbr3_2.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblFormattedNbr3_1
//
this.lblFormattedNbr3_1.Location = new System.Drawing.Point(26,
134);
this.lblFormattedNbr3_1.Name = "lblFormattedNbr3_1";
this.lblFormattedNbr3_1.Size = new System.Drawing.Size(194, 21);
this.lblFormattedNbr3_1.TabIndex = 9;
this.lblFormattedNbr3_1.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// lblFormattedNbr4_2
//
this.lblFormattedNbr4_2.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.lblFormattedNbr4_2.Location = new System.Drawing.Point(224,
160);
this.lblFormattedNbr4_2.Name = "lblFormattedNbr4_2";
this.lblFormattedNbr4_2.Size = new System.Drawing.Size(100, 21);
this.lblFormattedNbr4_2.TabIndex = 8;
this.lblFormattedNbr4_2.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblFormattedNbr4_1
//
this.lblFormattedNbr4_1.Location = new System.Drawing.Point(26,
160);
this.lblFormattedNbr4_1.Name = "lblFormattedNbr4_1";
this.lblFormattedNbr4_1.Size = new System.Drawing.Size(194, 21);
this.lblFormattedNbr4_1.TabIndex = 7;
this.lblFormattedNbr4_1.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// lblFormattedNbr2_2
//
this.lblFormattedNbr2_2.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.lblFormattedNbr2_2.Location = new System.Drawing.Point(224,
108);
this.lblFormattedNbr2_2.Name = "lblFormattedNbr2_2";
this.lblFormattedNbr2_2.Size = new System.Drawing.Size(100, 21);
this.lblFormattedNbr2_2.TabIndex = 6;
this.lblFormattedNbr2_2.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblFormattedNbr2_1
//
this.lblFormattedNbr2_1.Location = new System.Drawing.Point(26,
108);
this.lblFormattedNbr2_1.Name = "lblFormattedNbr2_1";
this.lblFormattedNbr2_1.Size = new System.Drawing.Size(194, 21);
this.lblFormattedNbr2_1.TabIndex = 5;
this.lblFormattedNbr2_1.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// lblFormattedNbr1_2
//
this.lblFormattedNbr1_2.BorderStyle =
System.Windows.Forms.BorderStyle.FixedSingle;
this.lblFormattedNbr1_2.Location = new System.Drawing.Point(224,
82);
this.lblFormattedNbr1_2.Name = "lblFormattedNbr1_2";
this.lblFormattedNbr1_2.Size = new System.Drawing.Size(100, 21);
this.lblFormattedNbr1_2.TabIndex = 4;
this.lblFormattedNbr1_2.TextAlign =
System.Drawing.ContentAlignment.MiddleCenter;
//
// lblFormattedNbr1_1
//
this.lblFormattedNbr1_1.Location = new System.Drawing.Point(26,
82);
this.lblFormattedNbr1_1.Name = "lblFormattedNbr1_1";
this.lblFormattedNbr1_1.Size = new System.Drawing.Size(194, 21);
this.lblFormattedNbr1_1.TabIndex = 3;
this.lblFormattedNbr1_1.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// button1
//
this.button1.Location = new System.Drawing.Point(224, 38);
this.button1.Name = "button1";
this.button1.TabIndex = 2;
this.button1.Text = "Format Nbr";
this.button1.Click += new
System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.AutoSize = false;
this.textBox1.Location = new System.Drawing.Point(116, 38);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(26, 38);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(84, 21);
this.label1.TabIndex = 0;
this.label1.Text = "Enter Number:";
this.label1.TextAlign =
System.Drawing.ContentAlignment.MiddleLeft;
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(604, 383);
this.Controls.Add(this.label8);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.StartPosition =
System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "Form1";
this.panel1.ResumeLayout(false);
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}

private void button1_Click(object sender, System.EventArgs e)
{
decimal nTestNbr = 0;

try
{
nTestNbr = decimal.Parse(this.textBox1.Text);

lblFormattedNbr1_1.Text = "ToString('#.##')";
lblFormattedNbr1_2.Text = nTestNbr.ToString("#.##");

lblFormattedNbr2_1.Text = "ToString('00.00')";
lblFormattedNbr2_2.Text = nTestNbr.ToString("00.00");

lblFormattedNbr3_1.Text = "ToString('#,#')";
lblFormattedNbr3_2.Text = nTestNbr.ToString("#,#");

lblFormattedNbr4_1.Text = "ToString('##-##-##')";
lblFormattedNbr4_2.Text = nTestNbr.ToString("##-##-##");

lblFormattedNbr5_1.Text = "ToString('(###)###-####')";
lblFormattedNbr5_2.Text =
nTestNbr.ToString("(###)###-####");

}
catch
{
MessageBox.Show("Invalid Number entered.\nPlease try
again\n\n");
}
}
}
}


==============================================================


"Jeffrey Tan[MSFT]" said:
Hi BBFrost,

Based on my understanding, you want to do the custom formatting for your
Numeric value.

I see that you have already found the answer to your question.
ToString("#.##") should work for you.

I think you may need a general specification of the numeric formatting.
Actually, in Numeric Format String, "#" is a digit placeholder, while "."
is a Decimal point.

For more information, please refer to:
"Custom Numeric Format Strings"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconcustomnumericformatstrings.asp

Also, there some samples may help you:
"Custom Numeric Format Strings Output Examples"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/htm
l/cpconcustomnumericformatstringsoutputexample.asp

=====================================
Please apply my suggestion above and let me know if it meets your need.

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi BBFrost,

Thanks very much for your feedback.

I am glad my reply makes sense to you. :)

Actually, I think MSDN has provided you much samples about the usage of
Net Framework classes. But because of the complexity of .Net Framework, we
can not add samples for all the classes and members. MSDN has included
normal usage of the most common classes.

Also, you may get some quick start of using .Net Framework from:
http://samples.gotdotnet.com/quickstart/

Anyway, www.google.com is always the most rich resouce for learning.

There is also a good article and sample site, for your information:
www.codeproject.com

Thank you for your patience and cooperation. If you have any questions or
concerns, please feel free to post it in the group. I am standing by to be
of assistance.

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Jeffrey Tan[MSFT]

Hi BBFrost,

Does my reply makes sense to you? Do you still have any concern?

Please feel free to let me know, I will help you. Thanks

Best regards,
Jeffrey Tan
Microsoft Online Partner Support
Get Secure! - www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top