formatting numbers

  • Thread starter Thread starter cj2
  • Start date Start date
C

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.pdf"
Instead I get "i:\collections\2008_0909\2013420470_20080909_2.pdf"

How is the best way to format my number for this use?
 
cj2 said:
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.pdf"
Instead I get "i:\collections\2008_0909\2013420470_20080909_2.pdf"

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)
 
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:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
Jeffrey said:
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.
 
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.
 
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.
 
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:
(e-mail address removed).

This posting is provided "AS IS" with no warranties, and confers no rights.
 
cj2 said:
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 said:
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.
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.
 
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.
 
cj2 said:
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 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 said:
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. :)
 
Back
Top