473,468 Members | 4,558 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Formating numbers

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
Jun 27 '08 #1
4 1341
BrassicaNigra wrote:
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.

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #2
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:
ms****@microsoft.com.

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.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/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.


--------------------
>From: =?Utf-8?B?QnJhc3NpY2FOaWdyYQ==?= <br************@community.nospam>
Subject: Formating numbers
Date: Sun, 8 Jun 2008 09:40:01 -0700
>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
Jun 27 '08 #3
"Steven Cheng [MSFT]" <st*****@online.microsoft.comwrote in message
news:Ft**************@TK2MSFTNGHUB02.phx.gbl...
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));

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.

--
Paul Hirose <jv********@earINVALIDthlink.net>
To reply by email remove INVALID

Jun 27 '08 #4
Paul Hirose wrote:
"Steven Cheng [MSFT]" <st*****@online.microsoft.comwrote in message
news:Ft**************@TK2MSFTNGHUB02.phx.gbl...
>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));


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));

--
Göran Andersson
_____
http://www.guffa.com
Jun 27 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: clequieu | last post by:
I am pulling numbers from a SQl Table and adding some together and doing other calculations. How do your format the numbers to insert a comma and show thousands?
6
by: BT | last post by:
I'm trying to format a three column table and I can't get things to line up the way I need to. The first column is text of variable length that might wrap onto multiple lines. It is within...
0
by: rodrigo guerra | last post by:
where i can change the code formating that visual studio does in the code.... like if i type: if ( ) { .... } visual studio changes to:
3
by: Smiley | last post by:
Hi, I know how to do confitional formating in Excel by clicking the wizard. Is this possible in MS Access ? If so, please directly where to look. I found example but nothing as to how to do it....
12
tolkienarda
by: tolkienarda | last post by:
hi all I am working on a content management service and i need some help keeping formating. what i am doing is recreating my site in an admin side and all of the articles on the site will...
1
by: Shawn Northrop | last post by:
I just read an article on kirupa.com about incorporating mySQL and PHP with flash. I am dynamically loading conent into a flash movie and am not sure how to format. In my php code i have: $y = "";...
0
by: Fonix | last post by:
I'm trying to make table border with pyExcelerator. As i can see there is only cell formating!? If i'm wrong pls tell me what is method to make more then one cell to have same format, other then...
2
by: sitko | last post by:
Hi, I'm in the process of converting a VB.net program into a C program so it can run on a unix like machine. I've been moving along at a nice pace, but this conversion has stumped me. I need...
5
by: cmdolcet69 | last post by:
Im having this problem with Formatting numbers.......In any case I declare a variable intvalue as double....when i look at what values are being written out in the text file i see two different...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.