msvisual.com Forum Index
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Padding with zeros

 
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Syntax
Author Message
Kelil



Joined: 04 Oct 2007
Posts: 2

PostPosted: Tue Oct 17, 2006 3:23 pm    Post subject: Padding with zeros Reply with quote

Hi all,

I have a simple syntax problem.
My Number needs to have 7 numerals with 2 decimals.
Eg.
0000.00
When the number is -8.19 it needs to be displayed as 00-8.19. I am able
to get it as -0008.19 by using Format(-8.19, "0000.00"). What am I
doing wrong/

Archived from group: microsoft>public>vb>syntax
Back to top
View user's profile Send private message
"Rick Rothstein \



Joined: 04 Oct 2007
Posts: 1584

PostPosted: Tue Oct 17, 2006 7:09 pm    Post subject: Re: Padding with zeros Reply with quote

> I have a simple syntax problem.
> My Number needs to have 7 numerals with 2 decimals.
> Eg.
> 0000.00
> When the number is -8.19 it needs to be displayed as 00-8.19. I am able
> to get it as -0008.19 by using Format(-8.19, "0000.00"). What am I
> doing wrong/

What you are doing wrong is assuming 00-8.19 is in anyway a standard display
format for negative numbers that it would be automatically supported in a
straight-forward manner within VB (or any programming language for that
matter). With that said, this should do what you want...

ZeroPaddedNumber = Right$("0000000" & Format$(Number, ".00"), 7)

In the more general case, you would use this...

FieldWidth = 7
DecimalPlaces = 2
ZeroPaddedNumber = Right$(String$(FieldWidth, "0") & Format$(-8.19, _
"." & String$(DecimalPlaces, "0")), FieldWidth)

Rick
Back to top
View user's profile Send private message
Kelil



Joined: 04 Oct 2007
Posts: 2

PostPosted: Wed Oct 18, 2006 10:08 am    Post subject: Re: Padding with zeros Reply with quote

Thank you very much. That really solved my problem.

Rick Rothstein (MVP - VB) wrote:
> > I have a simple syntax problem.
> > My Number needs to have 7 numerals with 2 decimals.
> > Eg.
> > 0000.00
> > When the number is -8.19 it needs to be displayed as 00-8.19. I am able
> > to get it as -0008.19 by using Format(-8.19, "0000.00"). What am I
> > doing wrong/
>
> What you are doing wrong is assuming 00-8.19 is in anyway a standard display
> format for negative numbers that it would be automatically supported in a
> straight-forward manner within VB (or any programming language for that
> matter). With that said, this should do what you want...
>
> ZeroPaddedNumber = Right$("0000000" & Format$(Number, ".00"), 7)
>
> In the more general case, you would use this...
>
> FieldWidth = 7
> DecimalPlaces = 2
> ZeroPaddedNumber = Right$(String$(FieldWidth, "0") & Format$(-8.19, _
> "." & String$(DecimalPlaces, "0")), FieldWidth)
>
> Rick
Back to top
View user's profile Send private message
Bob O`Bob



Joined: 04 Oct 2007
Posts: 1456

PostPosted: Wed Oct 18, 2006 3:57 pm    Post subject: Re: Padding with zeros Reply with quote

Kelil wrote:
> Thank you very much. That really solved my problem.
>



I disagree.
But it /did/ get you an answer resembling what you were looking for.

I'm really curious how and by whom that format could ever be considered valid or useful.



Bob
--
Back to top
View user's profile Send private message
HKSHK



Joined: 04 Oct 2007
Posts: 12

PostPosted: Fri Oct 20, 2006 7:10 pm    Post subject: Re: Padding with zeros Reply with quote

Dear Kelil,

This is what you are looking for:

Private Function HKSPad(ByVal Value As Currency, ByVal StringLength As
Byte) As String
Dim retVal As String
Dim t As Integer

retVal = Format(Value, "0.00")

For t = Len(retVal) To StringLength - 1
retVal = "0" & retVal
Next
HKSPad = retVal
End Function

Private Sub Command1_Click()
MsgBox HKSPad(-8.19, 7)
End Sub

> When the number is -8.19 it needs to be displayed as 00-8.19. I am
> able to get it as -0008.19 by using Format(-8.19, "0000.00"). What am
> I doing wrong/

But I wonder, what application would need such a value...

Best Regards,

HKSHK

Back to top
View user's profile Send private message
Display posts from previous:   
Related Topics:
How do you hold on to leading zeros in Numeric fields? I am pulling a series of numbers from a local database and I want to use them to qualify a query to the mainframe by writing them to a data set. The mainframe wants the fields to be char(3), which means I need the leading zeros added to the result of th

How can I trim off leading zeros from a number? Help greatly I am trying to trim (remove) off leading zero's from a number in a text box. The number may be anything from 0001 to 0999. Is this possible? Your help is greatly appreciated.

Get Border Padding in Vista Hi All, I was just trying to tweak a few of my apps to work better and display better in Vista but I need to know how to get the users choice for Windows Border Padding which it appears to defaults to 4. I expected to get this using but c

Hows VB6 with XML? Hi I'm just looking for some guidelines regarding VB6 and XML I've been asked to create a data reading interface for a customer and the incoming data is in XML files. I do not know much about XML, for me it's a script using tags similar to HTML and you ca

Is a printer still printng Is there a reliable way to know that? Thanks
Post new topic   Reply to topic    msvisual.com Forum Index -> VB Syntax All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group