Re: Price Data Formatting
- From: "Webbiz" <noreply@xxxxxxx>
- Date: Tue, 19 Aug 2008 12:57:21 -0500
Hi Mike.
Thanks for your comments.
Yes, I understand how the Format() function works. The exercise here,
however, is that the data format is NOT KNOWN during design time but is
based on the format of the data loaded during runtime.
As I posted elsewhere, my original solution to this problem was to read in a
sampling of the data to determine where the decimal point is located.
Then I would use that to select the correct sPattern() array element that
contains a matching pattern.
So sPattern(3) might have "#.000" as the string within so that I can use it
as...
sPrice = Format(sngPriceData, sPattern(iDecVal))
Usually when I ask a question on this forum someone ALWAYS has a better way
to do what I've done. So I ask in order to learn from experts like yourself.
I've come a long way because of you and others here (Rick, Steve, Larry,
(fill in the blank), etc. etc.).
Steve posted an alternative to my bulky way of doing this. Instead of
creating the array sPattern() to hold the different patterns, I can simply
use the String() function to concate "0" to a standard "0." pattern, or
"#.".
What do you think about that approach?
Also, is looping through some of the data and looking for the decimal point
location the correct/best approach to determining the existing format as I
have done? Or is there a easier/better way?
Thanks Mike.
Webbiz
"Mike Williams" <gagamomo@xxxxxxxxxxx> wrote in message
news:5cad10a4-e66a-46c6-9d5d-113b479fd6ba@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
On 18 Aug, 22:37, "Webbiz" <nore...@xxxxxxx> wrote:
What I have been doing now is loading the data and noting
the maximum digits found right of the decimal and left of the
decimal. How I do this is through a LOOP, using INSTR
I can't seem to get answers through the MS server at the moment so I'm
posting this from a Google Groups account. Anyone else having this
problem at the moment? Anyway, you don't need to do any of that stuff.
The Format function will do it all for you. Try this:
Private Sub Command1_Click()
Dim j As Single
j = 12.34
Print Format(j, "0.0000")
j = 0.345
Print Format(j, "0.0000")
End Sub
In the line Format(j, "0.0000") the first zero tells VB that you want
it to insert a leading zero before the decimal point if the number
does not have a "whole" part. Otherwise, if you did not want the
leading zero you would instead use Format(j, "#.0000"). The second
four zeros simply tell VB how many decimal digits you want to display
(four in this case) and VB will then add sufficient zeros to the
output to make it have the correct number of decimal places.
In this case the example is "hard coded" to format to four decimal
places, specified by the four zeros after the decimal point in the
line at(j, "#.0000"). However, if you do not know at design time how
many decimal places you require you can "build" the string "#.0000" at
runtime, inserting the appropriate number of zeros into the Format
instruction, so that you could for example build the string "#.000" if
your code decided at runtime that it wanted three decimal places. If
that's what you need to do and if you want any help with it then post
again.
When you print out the results of course, as the above simple example
does, the output is printed left aligned. If you instead want it to be
right aligned (as is often the case with such numbers) then you'll
need to add more code, but I haven't included any such code in this
example because much depends on how you are outputting the result
(printing it to a picturebox or a printer or something or perhaps
displaying it in some kind of control such as a ListBox or a Label or
whatever)
Mike
.
- Follow-Ups:
- Re: Price Data Formatting
- From: Mike Williams
- Re: Price Data Formatting
- References:
- Price Data Formatting
- From: Webbiz
- Re: Price Data Formatting
- From: Mike Williams
- Price Data Formatting
- Prev by Date: Re: Price Data Formatting
- Next by Date: Re: Price Data Formatting
- Previous by thread: Re: Price Data Formatting
- Next by thread: Re: Price Data Formatting
- Index(es):
Relevant Pages
|
Loading