By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,837 Members | 1,797 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,837 IT Pros & Developers. It's quick & easy.

formatting numbers

P: n/a
cj2
pages = 1

pages.tostring gives me "1"
pages+1.tostring gives me "2.0"

Why did it add the ".0" on there? I was looking for "2" and this screws
me up.

Actually this is what I was doing. In the end what I want is to print a
two digit number with leading 0s.

dim collLetterName as string
collLetterName = "i:\collections\" & Format(Now, "yyyy_MMdd") & "\" & _
thisCustno & "_" & Format(Now, "yyyyMMdd") & "_" & _
pages + 1.ToString.PadLeft(2, "0") & ".pdf"

It should produce "i:\collections\2008_0909\2013420470_20080909_02.p df"
Instead I get "i:\collections\2008_0909\2013420470_20080909_2.pd f"

How is the best way to format my number for this use?
Sep 9 '08 #1
Share this Question
Share on Google+
13 Replies


P: n/a
cj2 wrote:
pages = 1

pages.tostring gives me "1"
pages+1.tostring gives me "2.0"

Why did it add the ".0" on there? I was looking for "2" and this screws
me up.

Actually this is what I was doing. In the end what I want is to print a
two digit number with leading 0s.

dim collLetterName as string
collLetterName = "i:\collections\" & Format(Now, "yyyy_MMdd") & "\" & _
thisCustno & "_" & Format(Now, "yyyyMMdd") & "_" & _
pages + 1.ToString.PadLeft(2, "0") & ".pdf"

It should produce "i:\collections\2008_0909\2013420470_20080909_02.p df"
Instead I get "i:\collections\2008_0909\2013420470_20080909_2.pd f"

How is the best way to format my number for this use?
Perhaps because you are calling PadLeft with a string instead of a char...

When you format the date more than once, get it into a variable so that
you use the same value. You don't want the date to change between the
calls to Now so that you get the file in the wrong folder...

I would prefer to do it like this:

collLetterName =
String.Format("i:\collections\{0:yyyy_MMdd}\{1}_{0 :yyyyMMdd}_{2:00}.pdf",
Now, thisCustno, pages + 1)

--
Göran Andersson
_____
http://www.guffa.com
Sep 9 '08 #2

P: n/a
Hi cj,

Based on my test, "pages + 1.ToString()" will output "2" instead of "2.0".
My test code is listed below:
Dim pages As Integer = 1
Console.WriteLine(pages.ToString())
Console.WriteLine(pages + 1.ToString())

Regarding your unexpected formatting output, I think that is because
"PadLeft(2, "0")" method is applied on "1.ToString()" instead of "pages+1"
entirely. So if you apply PadLeft() to them together, it will generate the
formatting as you want, like this:

collLetterName = "i:\collections\" & Format(Now, "yyyy_MMdd") & "\" & _
thisCustno & "_" & Format(Now, "yyyyMMdd") & "_" & _
(pages + 1).ToString.PadLeft(2, "0") & ".pdf"

Sure, using String.Format() to control the formatting as Andersson provided
instead of using PadLeft() method gives a clearer code logic.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
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****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 10 '08 #3

P: n/a
.... & Format(pages + 1,"00") & ...

"cj2" <cj*@nospam.nospamwrote in message
news:eK***************@TK2MSFTNGP06.phx.gbl...
pages = 1

pages.tostring gives me "1"
pages+1.tostring gives me "2.0"

Why did it add the ".0" on there? I was looking for "2" and this screws
me up.

Actually this is what I was doing. In the end what I want is to print a
two digit number with leading 0s.

dim collLetterName as string
collLetterName = "i:\collections\" & Format(Now, "yyyy_MMdd") & "\" & _
thisCustno & "_" & Format(Now, "yyyyMMdd") & "_" & _
pages + 1.ToString.PadLeft(2, "0") & ".pdf"

It should produce "i:\collections\2008_0909\2013420470_20080909_02.p df"
Instead I get "i:\collections\2008_0909\2013420470_20080909_2.pd f"

How is the best way to format my number for this use?
Sep 10 '08 #4

P: n/a
Jeffrey Tan[MSFT] wrote:
Hi cj,

Based on my test, "pages + 1.ToString()" will output "2" instead of "2.0".
My test code is listed below:
Dim pages As Integer = 1
Console.WriteLine(pages.ToString())
Console.WriteLine(pages + 1.ToString())

Regarding your unexpected formatting output, I think that is because
"PadLeft(2, "0")" method is applied on "1.ToString()" instead of "pages+1"
entirely.
I see. The 1 is formatted as "01", then implicitly parsed into the
double value 1.0, then the integer value from pages is implicitly
converted to double so that it can be added to the 1.0, giving the
result 2.0, which then is implicitly converted into the string "2.0".

cj2, you should use Option Strict On, that keeps the compiler from
blindly creating all these implicit conversions. Instead you get a
squiggly line under "1.ToString", telling you that it can't be
implicitly converted to double, giving a clear hint of what's going on.

--
Göran Andersson
_____
http://www.guffa.com
Sep 10 '08 #5

P: n/a
cj2
Thanks, that's much better. I'm not too good with Format. But I did
find the flaw in my logic yesterday--read response to Goran Andersson.

James Hahn wrote:
... & Format(pages + 1,"00") & ...

"cj2" <cj*@nospam.nospamwrote in message
news:eK***************@TK2MSFTNGP06.phx.gbl...
>pages = 1

pages.tostring gives me "1"
pages+1.tostring gives me "2.0"

Why did it add the ".0" on there? I was looking for "2" and this
screws me up.

Actually this is what I was doing. In the end what I want is to print
a two digit number with leading 0s.

dim collLetterName as string
collLetterName = "i:\collections\" & Format(Now, "yyyy_MMdd") & "\" & _
thisCustno & "_" & Format(Now, "yyyyMMdd") & "_" & _
pages + 1.ToString.PadLeft(2, "0") & ".pdf"

It should produce "i:\collections\2008_0909\2013420470_20080909_02.p df"
Instead I get "i:\collections\2008_0909\2013420470_20080909_2.pd f"

How is the best way to format my number for this use?
Sep 10 '08 #6

P: n/a
cj2
Dummy me. I completely overlooked the 1.tostring vs (pages+1).tostring.
I should have known better.

I regret that the format given by Goran in his first message is
exceedingly confusing to me and I'm not so sure he didn't forget a piece
of the formatting I need, but that's ok I'm going to stick with
something that fits my programming style. I will however use the format
given by James and I really need to get better at the use of the format
command.
Jeffrey Tan[MSFT] wrote:
Hi cj,

Based on my test, "pages + 1.ToString()" will output "2" instead of "2.0".
My test code is listed below:
Dim pages As Integer = 1
Console.WriteLine(pages.ToString())
Console.WriteLine(pages + 1.ToString())

Regarding your unexpected formatting output, I think that is because
"PadLeft(2, "0")" method is applied on "1.ToString()" instead of "pages+1"
entirely. So if you apply PadLeft() to them together, it will generate the
formatting as you want, like this:

collLetterName = "i:\collections\" & Format(Now, "yyyy_MMdd") & "\" & _
thisCustno & "_" & Format(Now, "yyyyMMdd") & "_" & _
(pages + 1).ToString.PadLeft(2, "0") & ".pdf"

Sure, using String.Format() to control the formatting as Andersson provided
instead of using PadLeft() method gives a clearer code logic.

Thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
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****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.
Sep 10 '08 #7

P: n/a
Thansk for letting us know that the information was useful.

"cj2" <cj*@nospam.nospamwrote in message
news:eM**************@TK2MSFTNGP05.phx.gbl...
Thanks, that's much better. I'm not too good with Format. But I did find
the flaw in my logic yesterday--read response to Goran Andersson.

James Hahn wrote:
>... & Format(pages + 1,"00") & ...

"cj2" <cj*@nospam.nospamwrote in message
news:eK***************@TK2MSFTNGP06.phx.gbl...
>>pages = 1

pages.tostring gives me "1"
pages+1.tostring gives me "2.0"

Why did it add the ".0" on there? I was looking for "2" and this screws
me up.

Actually this is what I was doing. In the end what I want is to print a
two digit number with leading 0s.

dim collLetterName as string
collLetterName = "i:\collections\" & Format(Now, "yyyy_MMdd") & "\" & _
thisCustno & "_" & Format(Now, "yyyyMMdd") & "_" & _
pages + 1.ToString.PadLeft(2, "0") & ".pdf"

It should produce "i:\collections\2008_0909\2013420470_20080909_02.p df"
Instead I get "i:\collections\2008_0909\2013420470_20080909_2.pd f"

How is the best way to format my number for this use?
Sep 10 '08 #8

P: n/a
Hi cj,

No problem. This type of operator priority issue is always hard to
remember. In this case, the "." operator has higher priority than "+" which
caused the problem.

Anyway, if you need further help, please feel free to post, thanks.

Best regards,
Jeffrey Tan
Microsoft Online Community Support
=========================================
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****@microsoft.com.

This posting is provided "AS IS" with no warranties, and confers no rights.

Sep 11 '08 #9

P: n/a
cj2 wrote:
Dummy me. I completely overlooked the 1.tostring vs (pages+1).tostring.
I should have known better.
You overlooked using Option Strict On, that would have kept the compiler
from making so many conversions to be able to keep from doing what you
intended with the code...
I regret that the format given by Goran in his first message is
exceedingly confusing to me and I'm not so sure he didn't forget a piece
of the formatting I need, but that's ok I'm going to stick with
something that fits my programming style. I will however use the format
given by James and I really need to get better at the use of the format
command.
Here's an explanation of the formatting string:

i:\collections\ - some literal text
{0:yyyy_MMdd} - insert the first argument (Now) as 2008_0911
\ - literal
{1} - insert the second argument (thisCustno)
_ - literal
{0:yyyyMMdd} - insert the first argument (Now) as 20080911
_ - literal
{2:00} - insert the thirt argument (pages + 1) as 01
..pdf - literal

--
Göran Andersson
_____
http://www.guffa.com
Sep 11 '08 #10

P: n/a
cj2


Göran Andersson wrote:
cj2 wrote:
>Dummy me. I completely overlooked the 1.tostring vs
(pages+1).tostring. I should have known better.

You overlooked using Option Strict On, that would have kept the compiler
from making so many conversions to be able to keep from doing what you
intended with the code...
Negative. I like option strict off.
>I regret that the format given by Goran in his first message is
exceedingly confusing to me and I'm not so sure he didn't forget a
piece of the formatting I need, but that's ok I'm going to stick with
something that fits my programming style. I will however use the
format given by James and I really need to get better at the use of
the format command.

Here's an explanation of the formatting string:

i:\collections\ - some literal text
{0:yyyy_MMdd} - insert the first argument (Now) as 2008_0911
\ - literal
{1} - insert the second argument (thisCustno)
_ - literal
{0:yyyyMMdd} - insert the first argument (Now) as 20080911
_ - literal
{2:00} - insert the thirt argument (pages + 1) as 01
.pdf - literal
I can indeed follow what it does but I still find it unnecessarily
complicated when simpler methods work just as well. I still appreciate
your attempt to help. Thanks.
Sep 11 '08 #11

P: n/a
On Thu, 11 Sep 2008 17:01:27 -0400, cj2 <cj*@nospam.nospamwrote:
>

Göran Andersson wrote:
>cj2 wrote:
>>Dummy me. I completely overlooked the 1.tostring vs
(pages+1).tostring. I should have known better.

You overlooked using Option Strict On, that would have kept the compiler
from making so many conversions to be able to keep from doing what you
intended with the code...
Negative. I like option strict off.
I very, very strongly urge you to use OPTION STRICT ON. Otherwise you
will make mistakes that the compiler does not show you.
Sep 11 '08 #12

P: n/a
cj2 wrote:
>

Göran Andersson wrote:
>cj2 wrote:
>>Dummy me. I completely overlooked the 1.tostring vs
(pages+1).tostring. I should have known better.

You overlooked using Option Strict On, that would have kept the
compiler from making so many conversions to be able to keep from doing
what you intended with the code...
Negative. I like option strict off.
And every time you post about a problem that would have been avoided by
turning it on, you will most likely be suggested to have it turned on... :)
>
>>I regret that the format given by Goran in his first message is
exceedingly confusing to me and I'm not so sure he didn't forget a
piece of the formatting I need, but that's ok I'm going to stick with
something that fits my programming style. I will however use the
format given by James and I really need to get better at the use of
the format command.

Here's an explanation of the formatting string:

i:\collections\ - some literal text
{0:yyyy_MMdd} - insert the first argument (Now) as 2008_0911
\ - literal
{1} - insert the second argument (thisCustno)
_ - literal
{0:yyyyMMdd} - insert the first argument (Now) as 20080911
_ - literal
{2:00} - insert the thirt argument (pages + 1) as 01
.pdf - literal
I can indeed follow what it does but I still find it unnecessarily
complicated when simpler methods work just as well. I still appreciate
your attempt to help. Thanks.
Personally I find a single formatting string easier than to format each
value separately and concatenate them.

Note that you should put the Now value in a variable if you are using it
more than once when formatting them separately. Otherwise it may change
it's value between the calls. (It doesn't happen very often, but that's
the difference between code that works and code that almost always works.)

--
Göran Andersson
_____
http://www.guffa.com
Sep 12 '08 #13

P: n/a
cj2


Göran Andersson wrote:
cj2 wrote:
>>

Göran Andersson wrote:
>>cj2 wrote:
Dummy me. I completely overlooked the 1.tostring vs
(pages+1).tostring. I should have known better.

You overlooked using Option Strict On, that would have kept the
compiler from making so many conversions to be able to keep from
doing what you intended with the code...
Negative. I like option strict off.

And every time you post about a problem that would have been avoided by
turning it on, you will most likely be suggested to have it turned on... :)
I'm used to that. Thanks. :)
>>
>>>I regret that the format given by Goran in his first message is
exceedingly confusing to me and I'm not so sure he didn't forget a
piece of the formatting I need, but that's ok I'm going to stick
with something that fits my programming style. I will however use
the format given by James and I really need to get better at the use
of the format command.

Here's an explanation of the formatting string:

i:\collections\ - some literal text
{0:yyyy_MMdd} - insert the first argument (Now) as 2008_0911
\ - literal
{1} - insert the second argument (thisCustno)
_ - literal
{0:yyyyMMdd} - insert the first argument (Now) as 20080911
_ - literal
{2:00} - insert the thirt argument (pages + 1) as 01
.pdf - literal
I can indeed follow what it does but I still find it unnecessarily
complicated when simpler methods work just as well. I still
appreciate your attempt to help. Thanks.

Personally I find a single formatting string easier than to format each
value separately and concatenate them.

Note that you should put the Now value in a variable if you are using it
more than once when formatting them separately. Otherwise it may change
it's value between the calls. (It doesn't happen very often, but that's
the difference between code that works and code that almost always works.)
Sep 12 '08 #14

This discussion thread is closed

Replies have been disabled for this discussion.