473,654 Members | 3,264 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

format string modifier for actual number of decimals

Hi,

Is there a way to specify on the predefined format strings like P and N that
you want displayed all the decimals in the number...

for example

3 will display 3
2.1 will display 2.1
3.21 will display 3.21

I know I can do it will a custom format like 0.##### but would rather if
these is a handier way...

Thanks

Donal
Nov 16 '05 #1
6 2607
What about using just ToString() without format parameter?

--

Carlos J. Quintero

MZ-Tools 4.0: Productivity add-ins for Visual Studio .NET
You can code, design and document much faster.
http://www.mztools.com
"Donal McWeeney" <ma****@newsgro up.nospam> escribió en el mensaje
news:eU******** ******@tk2msftn gp13.phx.gbl...
Hi,

Is there a way to specify on the predefined format strings like P and N
that you want displayed all the decimals in the number...

for example

3 will display 3
2.1 will display 2.1
3.21 will display 3.21

I know I can do it will a custom format like 0.##### but would rather if
these is a handier way...

Thanks

Donal

Nov 16 '05 #2
>Is there a way to specify on the predefined format strings like P and N that
you want displayed all the decimals in the number...


Since you can add an infinite number of zeros to the decimals (2.1,
2.10 or 2.1000000000000 0...), you need to specify what you mean by
"all the decimals".

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #3
Meant trailing non-zero decimals...

2.1
2.000034

Thanks

Donal
Nov 16 '05 #4
Unfortunately you lose the % formatting then...
Nov 16 '05 #5
"Donal McWeeney" <ma****@newsgro up.nospam> wrote in message news:eU******** ******@tk2msftn gp13.phx.gbl...
Is there a way to specify on the predefined format strings like P and N that you want displayed all the decimals in the number...
If you're using a floating-point number, realize that the number of decimal
places right of the decimal point are going to be difficult to determine due
to rounding or truncation error.
I know I can do it will a custom format like 0.##### but would rather if


I see, you're collecting obfuscated one-liners for a book: "101 Complicated
Ways to Format a Number in .NET."

Here's one approach,

string s = decimalValue.To String( "N20");
NumberFormatInf o nfi = CultureInfo.Cur rentCulture.Num berFormat;
Console.WriteLi ne(
String.Format( "{{0:N{0}}} ",
Math.Max(
0,
s.LastIndexOfAn y(
"123456789".ToC harArray( )
) - s.IndexOf( nfi.NumberDecim alSeparator)
)
), decimalValue
);

Hopefully this will convince you to go right ahead with the picture-pattern
format string you're already aware of.

Console.WriteLi ne( decimalValue.To String( "0.".PadRig ht( 20,'#') ) );
Derek Harmon
Nov 16 '05 #6
Meant trailing non-zero decimals...

2.1
2.000034


Right, but due to the inexact nature of floating point numbers [1],
the computer may think there are decimals there that you don't expect.
Imagine for example that 3.0 can't be represented exactly, and the
nearest you get is something like 3.0000000000000 1. Would you still
want to show all those zeros?
[1] See http://www.yoda.arachsys.com/csharp/floatingpoint.html

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #7

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

Similar topics

1
1500
by: kevininstructor | last post by:
The following was copied from a web site for learning formatting. Dim myInt As Integer = 100 Dim FormatPrice As String = String.Format("Price = |{0,10:c }|", myInt) Console.WriteLine(FormatPrice) The result shown is no number but the character c as in the format string. Is the format not valid in this context? Thanks for your assistance, Kevin
3
2430
by: Sansanee | last post by:
Hi, I am wondering if there is any format string for phone number. Just like those dd:mm:yyyy or C for currency. Can anyone enlighten me? Thank you in advance, Sunny
6
1672
by: Larry Lard | last post by:
I am trying to apply the following easy-to-state formatting rule to display a number: - If the number is positive or negative, display it in the 'default' way - If the number is zero, the output should be an empty string Now I know this is trivial to code, but the elegant solution would be passing a custom format string to ToString(). The problem is, I can't make one that works: There doesn't seem to be a way to combine the
2
3996
by: Netmonster | last post by:
All, How to format string add commas at thousands position, and hundred thousands position Example 1: I have string s = "1001"; or could be a random number I want the output to be 1,001. Example 2 : I have string s = "1000001"; or could be a random number I want the output to be 1,000,001.
3
12791
by: stathisgotsis | last post by:
Hello everyone, Trusting K&R2 i thought until recently that spaces are ignored in scanf's format string. Reading arguments to the contrary confused me a little. So i now ask: Is scanf("%d%d",...) different from scanf("%d %d",...) in the Standard's point of view? Thank you.
7
5473
by: Tony Girgenti | last post by:
Hello. Trying to develop VS2005, SP1 windows form VB program. When i try a statement like below and the data is "24.95", without the quotes of course, regPriceString equals "000000000025+" . regPriceString = String.Format("{0:000000000000+}", merchandiseDataRow.Item("RegPrice"))
3
7147
by: Jef Driesen | last post by:
How can I convert a date string to a number (e.g. a time_t value or a tm struct)? I know about the strptime function, but then I have to know the format string. And that is a problem. I'm trying to autoformat the contents of text entries in a GUI. For numbers, I'm converting the text representation to the appropriate type (using atoi, atof, ...) and converting the result back to text with the correct format (using sprintf). But this does...
7
13647
by: Andrus | last post by:
How to create format string for decimal data type which shows blank for zero and default format otherwize ? I tried format string "f;f;#" but this shows f for nonzero numbers. Andrus. using System.Windows.Forms;
9
2301
by: =?Utf-8?B?TmVpbEdvdHQ=?= | last post by:
I am trying to format a decimal number such as 0.0567 but forgot how to do formatting in C#. I tried the old C format escape sequence "\8.3f" the old C style but does not work. example: dMyNumber.tostring("\8.3f"); Can some one refresh how to do this? Thanks
0
8376
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
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
8708
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
8489
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
7307
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
6161
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
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1596
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.