473,785 Members | 2,639 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

format numeric to string - decimal places

I have a Decimal or a Double - your choice. I'd like to print the number as
string with a specified number of decimal places. Let's say my number
110.5, and I'd like to capture it in a string with 3 decimal places:
"110.500". How do you do this? My method below isn't working so hot.

Decimal dcTotal = 110.5;
string strMyString = (Decimal.Round( dcTotal, 3)).ToString();
//Do something with this string

Thanks in advance!

Mark
Nov 15 '05 #1
3 14828
Console.WriteLi ne("Number Formatting - {0:F3}", 111.5555);

--

This posting is provided "AS IS" with no warranties, and confers no rights.
"Mark" <fi************ **@umn.edu> wrote in message
news:uq******** ******@TK2MSFTN GP12.phx.gbl...
I have a Decimal or a Double - your choice. I'd like to print the number as string with a specified number of decimal places.

Nov 15 '05 #2
Mark wrote:
I have a Decimal or a Double - your choice. I'd like to print the number as
string with a specified number of decimal places. Let's say my number
110.5, and I'd like to capture it in a string with 3 decimal places:
"110.500". How do you do this? My method below isn't working so hot.

Decimal dcTotal = 110.5;
string strMyString = (Decimal.Round( dcTotal, 3)).ToString();
//Do something with this string


dcTotal.ToStrin g("0.000")

To be honest, I find it difficult to locate this information in the docs
- it takes a bit of digging past relatively useless stuff about
NumberFormatInf o and CultureInfo classes to get to the basic, predefined
format characters I want.

This information happens to be in the docs under the heading "Custom
Numeric Format Strings".

Note to MS: please put links to the standard and custom formatting
characters for the numeric and DataTime types in the document pages for
ToString() and String.Format() .
--
mikeb
Nov 15 '05 #3

"> > I have a Decimal or a Double - your choice. I'd like to print the
number as
....
To be honest, I find it difficult to locate this information in the docs

I agree on that.
Nov 15 '05 #4

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

Similar topics

2
15864
by: Bubba | last post by:
I know it's possible, just don't know how to do it. I have a spreadsheet that I imported into access. Two of the columns in the table have Hard Drive space values listed for example 2.45 GB and 453 MB, you get the picture. Both the GB and MB assorted values exist in both columns. Is there anyway to convert the GB or MB to 0's, or even better yet decimal places so I can look for values < or > to 6gb. I already tried a query, but it seems...
1
2536
by: MAF | last post by:
How can I set the number of decimal places for a double value?
4
9496
by: Phil Mc | last post by:
OK this should be bread and butter, easy to do, but I seem to be going around in circles and not getting any answer to achieving this simple task. I have numbers in string format (they are coming in from an excel sheet): Examples. 45,666.0041 5664456.12 -5465.25568 ETC
2
7000
by: hunslair | last post by:
This is a really basic question. I am taking a teach yourself beginners course on Access 2003 and have run into a road block on one practice. In a select query, I have created a simple calculation field. Markup:(-)/ which is to formatted as Percent with 0 Decimal places. When I click into the Format box is am offered
17
5286
by: scan87 | last post by:
Can somone please, please give me the solution for the following problem. I need to submit it on Monday. Write a global function called format, which formats numbers with a given number of decimal places. The function accepts a compulsory first argument of type double (the number to be formatted) and a second optional argument of type integer (the number of decimal places) and returns a string object containing the formatted text. If the...
3
1699
by: Andrus | last post by:
I have lot of columns in database like Price Numeric(5,2) Street Character(30) For UI display and validation I need to retrieve width and for numeric properties, decimal places of corresponding DbLinq property. pgsqlmetal generates code for column properies without width and decimal places like
3
1991
by: David | last post by:
Any ideas why this statement: mBalanceChange = Double.Parse(strCurrentBalance, Globalization.NumberStyles.AllowThousands Or Globalization.NumberStyles.AllowDecimalPoint) - mBalanceDouble would leave me with a double containing over 10 decimal places when strCurrentBalance was a string containing a double with two decimal places and mBalanceDouble contained only two decimal places to begin with?
4
2803
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - How do I format a Number as a String with exactly 2 decimal places? ----------------------------------------------------------------------- When formatting money for example, to format 6.57634 to 6.58, 6.5 to 6.50, and 6 to 6.00? Rounding of x.xx5 is uncertain, as such numbers are not represented exactly. See the section on "simple decimal arithmetic"
8
5472
NeoPa
by: NeoPa | last post by:
I have noticed this over many versions of Access and was wondering if anyone could throw any light on why this seems to happen. It seems that when designing a QueryDef in the QBE grid and specifying the Properties of an individual numeric field, the Decimal Places property is often invisible. From observation this seems to occur when the Field: is set to a calculation of some form (rather than a simple field from one of the TableDefs). ...
0
9481
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10336
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10155
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
10095
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
8978
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
7502
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
5513
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3655
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.