472,794 Members | 4,015 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

formatting numbers

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
13 1481
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
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
.... & 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
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
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Dan Sommers | last post by:
Hi, I have a class whose objects represent physical quantities including uncertainties and units, and I would like more control over the way they print. I have a __str__ method which outputs...
4
by: Tommi Mäkitalo | last post by:
Hi I need to format floating-point-numbers with exact 2 digits after decimal point. I could use printf with "%.2f", but it don't use std::locale. Any ideas? -- Tommi Mäkitalo
4
by: Dave Brydon | last post by:
Access 2003 I have a combo box in my personnel table, which draws its data from a trade code table; the original field in the code table, is numeric, Long Integer, and formatted with 5 zero's . ...
4
by: John Sutor | last post by:
I need some code that, on each keyup event, will take all of the numbers typed into the text box and format as they type to look like this 100 1,000 10,000 100,000 1,000,000 John S
7
by: ilona | last post by:
Hi all, I store phone numbers in the database as 123447775665554(input mask is used for input, and some numbers have extensions), and I also know from db if the number is Canadian, US, or some...
9
by: Markus | last post by:
Hi all, two questions on formatting numbers: - Given is a decimal/money datatype in SQL-Server, e.g. decimal(9,4), thus displaying a value like 13.2000 How can I prevent to display the...
5
by: Carla | last post by:
I have Access 2000. I made a mailing list database and for the life of me can't remember how to format the zip code field so it will print on the label as xxxxx-xxxx. I have tried various - 9, #,...
6
by: Rafael Olarte | last post by:
The goal of this project is to output the following information as follows: 34.5 38.6 4.1 42.4 3.8 close 46.8 4.4 big change. The values of the first colunm are obtain from a file...
2
by: jerryyang_la1 | last post by:
I need some advice on formatting numbers using PHP. My database holds numbers like: 10 0.2 I need these to be displayed as 10.00
3
by: sparks | last post by:
We have one database that they are constantly reformatting their inputs. I asked about the changes and they can never get the same types of numbers from the people. some are -.95 and later they...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.