473,473 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

String.format with numbers less than zero

Hello All,

I am using the following to format numbers precise to 2 decimal places
with thousand separators:

Double number = 100000.87;
string MyString = String.Format("{0:0,0.00}", number);
// 100,000.87
which is fine.

When the number is less than zero:
Double number = 0.8756;
string MyString = String.Format("{0:0,0.00}", number);
// 00.87
How do output 0.87 instead of 00.87 ?

Thanks in advance.

Feb 21 '08 #1
3 3890
Can try to use String.Format("{0:#,0.00}", number)
The "# " character will do it.

Alcides Schulz
http://alsql.blogspot.com
Feb 21 '08 #2
string MyString = number.ToString("N2");

/claes

"hharry" <pa*********@nyc.comwrote in message
news:e9**********************************@p25g2000 hsf.googlegroups.com...
Hello All,

I am using the following to format numbers precise to 2 decimal places
with thousand separators:

Double number = 100000.87;
string MyString = String.Format("{0:0,0.00}", number);
// 100,000.87
which is fine.

When the number is less than zero:
Double number = 0.8756;
string MyString = String.Format("{0:0,0.00}", number);
// 00.87
How do output 0.87 instead of 00.87 ?

Thanks in advance.

Feb 22 '08 #3

"hharry" <pa*********@nyc.comwrote in message
news:e9**********************************@p25g2000 hsf.googlegroups.com...
Hello All,

I am using the following to format numbers precise to 2 decimal places
with thousand separators:

Double number = 100000.87;
string MyString = String.Format("{0:0,0.00}", number);
// 100,000.87
which is fine.

When the number is less than zero:
Double number = 0.8756;
string MyString = String.Format("{0:0,0.00}", number);
// 00.87
How do output 0.87 instead of 00.87 ?

Thanks in advance.
Would you mind explaining when 0.8756 became less than zero?

Feb 22 '08 #4

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

Similar topics

2
by: Von Shean | last post by:
I use the following code to format the numbers to have commas. But in case when input is zero it returns an empty string....any ideas? String.Format("{0:#,###.##}",dblNum);
2
by: Bob | last post by:
I'm having trouble the string.Format() throwing exceptions and I can't figure out what I am doing wrong. Given the following setup code: string str = { "one", "two", "three", "four" }; double...
2
by: Akeel | last post by:
Hi I have the following code char splitter={','}; string fields=row.ToString().Split(splitter) string serviceDefintion="" // row.ToString().Split(splitter // conatins the followin...
32
by: tshad | last post by:
Can you do a search for more that one string in another string? Something like: someString.IndexOf("something1","something2","something3",0) or would you have to do something like: if...
11
by: drtimhill | last post by:
I'm just starting out on Python, and am stumped by what appears an oddity in the way negative indices are handled. For example, to get the last character in a string, I can enter "x". To get the...
232
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
7
by: Rick | last post by:
With String.Format, if I have an incorrect number of args specified for a format string, compile fails. How can I implement similar design-time functionality for my own string functions?
5
by: Xah Lee | last post by:
If i have a nested list, where the atoms are unicode strings, e.g. # -*- coding: utf-8 -*- ttt=, ,...] print ttt how can i print it without getting the u'\u1234' notation? i.e. i want it...
7
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. ...
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
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
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,...
1
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...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...
0
muto222
php
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.