473,396 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Integer String Formating not Working?


Very simple goal ... to string format an int with commas.

I have
int x = 12000

If I use

x.ToString("N")

I get 12,000.00

So I tried x.ToString("0,0")

I get 12,000. That's great!

But then if x is 1, I get "01"

That's not good ;-(

Tried a bunch of other stuff ... but don't get the desired results ... .

What am I doing wrong?
Nov 16 '05 #1
4 3200
Try this

int num = 12000;
string str = string.Format("{0,0:{{#,##}}}",num);

--
Shak
(Houston)
"msnews.microsoft.com" <bk****@liquidnet.com> wrote in message
news:uO*************@TK2MSFTNGP11.phx.gbl...

Very simple goal ... to string format an int with commas.

I have
int x = 12000

If I use

x.ToString("N")

I get 12,000.00

So I tried x.ToString("0,0")

I get 12,000. That's great!

But then if x is 1, I get "01"

That's not good ;-(

Tried a bunch of other stuff ... but don't get the desired results ... .

What am I doing wrong?

Nov 16 '05 #2
Hey Shak, didn't you get traded to the Miami Heat?

:)

Michael C.

"Shakir Hussain" <sh**@fakedomain.com> wrote in message
news:eS*************@tk2msftngp13.phx.gbl...
Try this

int num = 12000;
string str = string.Format("{0,0:{{#,##}}}",num);

--
Shak
(Houston)
"msnews.microsoft.com" <bk****@liquidnet.com> wrote in message
news:uO*************@TK2MSFTNGP11.phx.gbl...

Very simple goal ... to string format an int with commas.

I have
int x = 12000

If I use

x.ToString("N")

I get 12,000.00

So I tried x.ToString("0,0")

I get 12,000. That's great!

But then if x is 1, I get "01"

That's not good ;-(

Tried a bunch of other stuff ... but don't get the desired results ... .

What am I doing wrong?


Nov 16 '05 #3
If you have a int x, then ToSTring.("N0") should give you 12,000.

For details on formatting see:

http://msdn.microsoft.com/library/de...ttingtypes.asp

Hope this helps
Jay

"msnews.microsoft.com" <bk****@liquidnet.com> wrote in message
news:uO*************@TK2MSFTNGP11.phx.gbl...

Very simple goal ... to string format an int with commas.

I have
int x = 12000

If I use

x.ToString("N")

I get 12,000.00

So I tried x.ToString("0,0")

I get 12,000. That's great!

But then if x is 1, I get "01"

That's not good ;-(

Tried a bunch of other stuff ... but don't get the desired results ... .

What am I doing wrong?

Nov 16 '05 #4
Yes Jay that helped alot!

Shak, the difference between the 0 and # is summed up on this page ...
http://msdn.microsoft.com/library/de...matstrings.asp

For my needs ... I needed the zero to stick around ... so I wen't with the
"N0"

Thank you guys very much!
"Jay B. Harlow [MVP - Outlook]" <Ja************@msn.com> wrote in message
news:ui**************@TK2MSFTNGP12.phx.gbl...
If you have a int x, then ToSTring.("N0") should give you 12,000.

For details on formatting see:

http://msdn.microsoft.com/library/de...ttingtypes.asp
Hope this helps
Jay

"msnews.microsoft.com" <bk****@liquidnet.com> wrote in message
news:uO*************@TK2MSFTNGP11.phx.gbl...

Very simple goal ... to string format an int with commas.

I have
int x = 12000

If I use

x.ToString("N")

I get 12,000.00

So I tried x.ToString("0,0")

I get 12,000. That's great!

But then if x is 1, I get "01"

That's not good ;-(

Tried a bunch of other stuff ... but don't get the desired results ... .

What am I doing wrong?


Nov 16 '05 #5

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

Similar topics

13
by: coinjo | last post by:
Is there any function to determine the length of an integer string?
10
by: John Salerno | last post by:
Can someone tell me what's happening here. This is my code: PUNCT_SPACE_SET = set(string.punctuation + string.whitespace) def filter_letters(original): return ''.join(set(original) -...
5
by: f pemberton | last post by:
I have kind of an interesting string, it looks like a couple hundred letters bunched together with no spaces. Anyway, i'm trying to put a "?" and a (\n) newline after every 100th character of the...
1
by: Stile213 | last post by:
What's the easiest way to input an integer string using the cin function? The string will be single digits and from 1 to 5 elements long.
3
by: Steven D'Aprano | last post by:
Problem: I have an application where I need to print integers differently depending on whether they are positive or negative. To be more specific, I have to print something that looks like: ...
8
by: GHUM | last post by:
imagine: template=""" Hello %(name)s, how are you %(action)s""" we can use it to do things like: print template % dict (name="Guido", action="indenting")
5
by: Jeremy Kitchen | last post by:
Private mFormat As String = "##.##" Return DirectCast(value, Double).ToString(mFormat) returns "" when value is 0.0 I would prefer that it return "0". Does anyone have some advice? Thanks...
7
by: Jim | last post by:
Hi guys, The variable '$pixels' in the code below may be an integer type or an integer represented as a string type. I need to check if the value is indeed one of these. Is the following code...
1
by: nico | last post by:
Hi, I need to do a lot of string formating, and I have strings and/or unicode strings and when I do the following: "%s %s" % (u'Salut', 'H\xe4llo'), I get an exception : UnicodeDecodeError:...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
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...
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,...

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.