Formating numbers

B

BrassicaNigra

Greetings,

I am writing an application that prepares a quotation for a customer. The
calculations are done in a SQL Server stored procedure. I use a datareader
to fetch the record, but cannot seem to get the data to format correctly.
Since it is a quote I want the data in the format 12.34. Everything seems
fine, except when I have a a zero at the end of the price, then I get 12.3.
It needs to be 12.30.

When I set the textbox value using the datareader I was using the ToString()
function to convert the data from a floating point to a string. Usually when
I do this I can apply a format string as an argument (i.e. ToString("F2")).
When I use the ToString method of the datareader it does not allow for any
arguments.

Can't seem to find any information about this. I have spent more time
trying to format these prices than I have writing the entire application.
What happened to simple data formatting (like the old sprintf())?

Help!

Dale Hoffman
 
G

Göran Andersson

BrassicaNigra said:
Greetings,

I am writing an application that prepares a quotation for a customer. The
calculations are done in a SQL Server stored procedure. I use a datareader
to fetch the record, but cannot seem to get the data to format correctly.
Since it is a quote I want the data in the format 12.34. Everything seems
fine, except when I have a a zero at the end of the price, then I get 12.3.
It needs to be 12.30.

When I set the textbox value using the datareader I was using the ToString()
function to convert the data from a floating point to a string. Usually when
I do this I can apply a format string as an argument (i.e. ToString("F2")).
When I use the ToString method of the datareader it does not allow for any
arguments.

You should not use the ToString method of the data reader, it will most
likely just return the type name of the data reader object anyway. You
should use the ToString method on the data that you read from the data
reader.

Example:

SomeTextBox.Text = reader.GetDouble(0).ToString("F2");
Can't seem to find any information about this. I have spent more time
trying to format these prices than I have writing the entire application.
What happened to simple data formatting (like the old sprintf())?

It's hasn't changed very much. Take a look at the string.Format method,
it works pretty much like sprintf, only the formatting syntax is a bit
different.
 
S

Steven Cheng [MSFT]

Hi Dale,

As Goran suggested, you should first get the strong-typed data object(such
as double or int ....), then performing formatting with the "toString"
function fo the strong-typed data object. Here is the number formatting
reference in .net:

#Standard Numeric Format Strings
http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

BTW, you can also use string.Format function to format multiple output
objects.e.g.

===================
double dv = 343.4343;

Console.WriteLine(string.Format("{0:f3}", dv));
=============

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.




--------------------
 
G

Göran Andersson

Paul said:
Is there a way to control the precision at run time? Sometimes the
appropriate precision isn't known at compile time.

In C you can print double d with 3 decimal places like this:

printf("%.3f\n", d);

And if the precision needs to be set at run time, you get i decimal
places like this:

printf("%.*f\n", i, d);

I couldn't figure out how to do that in C#, so I had to construct the
control string with a StringBuilder. It worked, but the old fashioned
printf() would have been a lot cleaner.

The .NET formatting doesn't support formatting in the formatting, as the
printf function does. Using string operations to construct a format
string is the way to do it.

If you are formatting just a single value, you can use a ToString
overload instead of the string.Format method:

double dv = 343.4343;
int decimals = 3;
string format = "f" + decimals.ToString();

Console.WriteLine(dv.ToString(format));
 

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