473,624 Members | 2,615 Online
Bytes | Software Development & Data Engineering Community
+ 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 1350
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.Tex t = reader.GetDoubl e(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.WriteLi ne(string.Forma t("{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****@microsof t.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?QnJhc3NpY2F OaWdyYQ==?= <br************ @community.nosp am>
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.comw rote in message
news:Ft******** ******@TK2MSFTN GHUB02.phx.gbl. ..
BTW, you can also use string.Format function to format multiple
output
objects.e.g.

double dv = 343.4343;

Console.WriteLi ne(string.Forma t("{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********@ear INVALIDthlink.n et>
To reply by email remove INVALID

Jun 27 '08 #4
Paul Hirose wrote:
"Steven Cheng [MSFT]" <st*****@online .microsoft.comw rote in message
news:Ft******** ******@TK2MSFTN GHUB02.phx.gbl. ..
>BTW, you can also use string.Format function to format multiple
output
objects.e.g.

double dv = 343.4343;

Console.WriteL ine(string.Form at("{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.ToStri ng();

Console.WriteLi ne(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
467
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
1822
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 <p></p> tags to right/left justify it. The second column is a column of ditto marks, centered left/right in the cell and aligned with the bottom line of text of column 1. The third column is a column of numbers also aligned with the bottom line of...
0
1471
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
2110
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. I am very green in this. Please help.
12
1852
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 now be in <textarea> boxes so admins can edit them and save the changes it is working but any formating they use dosen't seem to work. if they want to have a return in the middle of their article the only way it will work is if they enter a br tag in...
1
2544
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 = ""; foreach($result as $x){ $y = $y . $x . " "; } print "myVar = $y"; In short $result is a query and $y is the value i will be passing along to flash. the statement $y = $y . $x . " "; puts a space between each name that is passed along. I am...
0
1745
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 just give them one-by-one. For little tables 100/20 formating cell by cell work ( i try with borders) fine. But when i get 5000/20 excel say: Too many different cell format! and excel remove some of the formats :( Pls help i need to format...
2
1744
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 a function to take two arguments:(a double number which may or may not be an integer, and an integer which will be the number of digits to store after the decimal place) it needs to turn this into a string which is always 10 characters long. if the...
5
1204
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 values.the first value is 0.01 and the second value is 0.0099999998 how can I format the number to 2 decimal places???
0
8631
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8341
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8490
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7174
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6112
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5570
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4084
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4184
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1489
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.