Format a number

  • Thread starter Thread starter AHopper
  • Start date Start date
A

AHopper

I am using the following to format a number in a message
box " & Format(Nz(.BatchNumber), "000000000000") & "

In the text box the number shows as 022802160407 but in
the message box it shows as 22802160407 (missing the first
0). How can I get the number to show 0 when it starts with
one or more of them.

Thanks
Allan
 
Allan,

Format(Nz(.BatchNumber, 0) "000000000000")

Regards,
Graham R Seach
Microsoft Access MVP
Sydney, Australia
 
I am using the following to format a number in a message
box " & Format(Nz(.BatchNumber), "000000000000") & "

In the text box the number shows as 022802160407 but in
the message box it shows as 22802160407 (missing the first
0). How can I get the number to show 0 when it starts with
one or more of them.

Thanks
Allan

What's the purpose of the dot in Nz(.BatchNumber)?

Try
& Format(Nz([BatchNumber],0),"000000000000")

or as another alternative:
& String(12-Len(Nz([BatchNumber],0)),"0") & [BatchNumber]
 
I am using the following to format a number in a message
box " & Format(Nz(.BatchNumber), "000000000000") & "

In the text box the number shows as 022802160407 but in
the message box it shows as 22802160407 (missing the first
0). How can I get the number to show 0 when it starts with
one or more of them.

Thanks
Allan

What's the purpose of the dot in Nz(.BatchNumber)?

Try
& Format(Nz([BatchNumber],0),"000000000000")

or as another alternative:
& String(12-Len(Nz([BatchNumber],0)),"0") & [BatchNumber]

I forgot to add to my previous message...
If [BatchNumber] is not to be used in any mathematical formula, you
should change it's datatype to Text. Text Datatype does not truncate
leading Zero's from the field as a number Datatype does.
 
I am using the following to format a number in a message
box " & Format(Nz(.BatchNumber), "000000000000") & "

In the text box the number shows as 022802160407 but in
the message box it shows as 22802160407 (missing the first
0). How can I get the number to show 0 when it starts with
one or more of them.

Thanks
Allan

I would VERY strongly recommend storing the BatchNumber in a Text
field, not a Number. A long integer is limited to two billion odd
(your number is bigger); a Float doesn't have enough precision to
handle twelve digits; and a Double will have roundoff error.

You'll never be doing math with this value. Just use Text and you can
put it into a message box directly.
 
Hi Allan

What is the data type of the BatchNumber field? It can't be long integer,
because the value is too large. I'm guessing that it is being treated as a
string. You could try converting it to a Decimal as follows:

Format(CDec(Nz(.BatchNumber,0)), "000000000000")
 
Graham, Sorry for not replying sooner, the last few days
have been very busy to say the least.
Thank you very much for you help
Allan
 
John, hope you see this message. Sorry I didn't respond
sooner but the last few days have been very busy. I need
to understand more about the various number types. I have
been using mostly Long Integer and Double, but do not have
a proper grasp of when to use them.

Thanks again
Allan
 
Graham,sorry for the late reply. BatchNumber is a Double
number field.
Thank you very much for your help
Allan
-----Original Message-----
Hi Allan

What is the data type of the BatchNumber field? It can't be long integer,
because the value is too large. I'm guessing that it is being treated as a
string. You could try converting it to a Decimal as follows:

Format(CDec(Nz(.BatchNumber,0)), "000000000000")

--
Good Luck!

Graham Mandeno [Access MVP]
Auckland, New Zealand

I am using the following to format a number in a message
box " & Format(Nz(.BatchNumber), "000000000000") & "

In the text box the number shows as 022802160407 but in
the message box it shows as 22802160407 (missing the first
0). How can I get the number to show 0 when it starts with
one or more of them.

Thanks
Allan


.
 
John, hope you see this message. Sorry I didn't respond
sooner but the last few days have been very busy. I need
to understand more about the various number types. I have
been using mostly Long Integer and Double, but do not have
a proper grasp of when to use them.

An Integer is a 16-bit number; it allows integral numeric values
between -65536 and 65535. A Long Integer is 32 bits - it is good for
the range -2147483648 to 2147483647, again just integers, no decimals.

Float and Double are "floating point numbers". This is a way of
storing a number in two portions - a binary fraction between 0.5 and
1, and an exponent: a power of two. A Float occupies 32 bits (24 bits
for the fraction, 8 for the exponent), allowing about seven decimal
digits of precision and a range between 10^-38 to 10^38, positive or
negative. A Double is similar but twice as many bits, covering the
range from -1.797*10^308 to 1.797*10^308, with about 14 digits of
precision. Finally a Currency datatype is a special scaled huge
integer, with a range in the trillions and exactly four decimal
places, no more and no fewer.

NONE of these datatypes are appropriate for identifiers such as part
"numbers". If you have a (say) twelve-digit identifier, and you'll
never be doing addition or subtraction or multiplication or division
with it, just store the identifier in a Text datatype. This gives you
up to 255 "digits" of precision, never has roundoff error, and lets
you display leading zeros with no special handling.
 
John, if I use the text datatype, can I restrict users so
they can only enter numbers in that field?

Thank you for your explaination of the number datatypes. I
jhave looked for a clear explaination but never was able
to find what you made so clear. I am going to keep it as a
quick reference.

Allan
 
John, if I use the text datatype, can I restrict users so
they can only enter numbers in that field?

Yes - use an Input Mask of

0000000000000000

for instance; this will force the entry of exactly sixteen numeric
digits, and prohibit any other characters.

And thanks for the kind words - glad to be able to pass on some things
I first learned forty years ago! (Yep, 16-bit and 32-bit integers and
Floating Point do indeed go back that far).
 
Back
Top