Re: Price Data Formatting

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Maybe I'm just dense (that's no surprise), but this doesn't appear to
address the problem to be solved.

I'm loading into the application price data that has a format I do not know
about in advance.

For example, if the program loads in data that appears to be 'MOSTLY' one
digit to the left of the decimal and 3 to the right, I want all numbers that
result from any calculations on this data to also be in the same format as
the data loaded.

Since price data format is all dependent on the market selected (say IBM
prices or British Pound prices), the formats are different.

With IBM, it will likely be 1 to 3 digits to the left of the decimal and
only 2 following the decimal. So all resulting calculations would need to be
rounded/truncated to 2 digits following the decimal as well.

But with British Pound, it only have 1 digit to the left of the decimal and
4 to the right, like (1.8657). Therefore, any calculations done on this data
should produce results formatted to only 4 digits to the right of the
decimal.

So what I had done before to solve this was to first create an array of
'patterns'.

Dim sPattern(1 to 4) as string

sPattern(1) = "#.0"
sPattern(2) = "#.00"
sPattern(3) = "#.000"
sPattern(4) = "#.0000"

Then I'd check a sampling of the data to make sure that the majority of the
data was the same (as some will be different if following the decimal is a
zero value, such as 1.1230 would only show as 1.123, which would give a
..000 indication rather than the required .0000).

So with this sampling, I'd find that based on the LEN and where the decimal
falls INSTR, that this result would pick the sPattern element to be used in
all Format() uses on displayed data.

But I'm thinking that I'm going about this process all wrong, that there is
a MORE PROFESSIONAL way to do this as a VB programmer. You see, I have solve
many of my problems building my application over the years. However, I
usually find 'rookie' solutions and so I come here to ask you professionals
so that I can become a better VB programmer. :-)

Well, that's the problem. Or is it a problem or the correct way to do this?

Thank you all.

Webbiz




"Randem" <msnewsgroup@xxxxxxxxxx> wrote in message
news:%23D0vGKbAJHA.1748@xxxxxxxxxxxxxxxxxxxxxxx
Something like this:

Dim sStr As String
Dim sFormat As String
Dim Number As Double

Number = "123.56"
sFormat = "###.##"

sStr = Format(Number, sFormat)
MsgBox sStr

You can adjust the sFormat to the format you determine your numbers to be
in.

--
Randem Systems, Inc.
Makers of InnoScript for Inno Setup.
It creates the script for you from a vbp, vbg, vbproj or setup.lst file.
http://www.innoscript.com/innoscript.html
http://www.randem.com/installerproblems.html
"Webbiz" <noreply@xxxxxxx> wrote in message
news:eGloVzaAJHA.1016@xxxxxxxxxxxxxxxxxxxxxxx
Thanks Ralph.

But you lost me. :)

Webbiz


"Ralph" <nt_consulting64@xxxxxxxxx> wrote in message
news:eZM14YaAJHA.4312@xxxxxxxxxxxxxxxxxxxxxxx

"Webbiz" <noreply@xxxxxxx> wrote in message
news:eQbrcqXAJHA.1184@xxxxxxxxxxxxxxxxxxxxxxx
Hello.

I've created what I consider some pretty novice ways to format my price
data.

But I can't help thinking there is a better way to do this.

For example, suppose I am loading price data that appears to be a
decimal
and 4 digits like this (.1234).

The program would need to determine that all results produced from
these
values must also have 4 digits after the decimal, even if the answer is
just
.123 or 23.

The .123 should then display as .1230 and the 23 should display as
23.0000,
both cases where 4 digit places exist in the display following the
decimal
only because the data loaded appears to be in that form.

I might load data that looks like this:

3.4567
2.3482
5.2849
6.123
1.3847


This tells me that the format I want is a digit prior to the decimal
and 4
digits following, even though some of the data loaded actualy had only
3
digits following the decimal. The maximum format from the sample is
1.1234
format. Therefore, I want to format all my resulting calculations in
the
same 1.1234 format. Of course the result can be greater than 1 digit
left
of
the decimal, but not greater than 4 following in this case.

If my sample data is:

23.45
83.83
22.2
48.89

This tells me I need to format 2 places following the decimal and AT
LEAST
2
prior.

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 to find where in the price
string the decimal is located, and using the LEN. But then I have to
figure
out how to make the changes in the Format() to display all my data in
that
format discovered, so I create a few Format()'s with "0.0", "0.00",
"0.000",
"#.0000", etc. etc.

This seems pretty hookie to me.

Any suggestions on a better way to do this?

Thanks.

Webbiz


You could use the Currency datatype.
Wrap it in a class that has a format method to return a space terminated
string.

-ralph








.



Relevant Pages

  • Re: Text formatted cells displaying numbers in scientific format
    ... When it 'sees' a large number it uses scientific format since ... I accept that Excel can't handle numbers greater than 15 digits and your ... string was 16 digits or more to be converted to a scientific value. ... It just doesn't display correctly in the cell (except after entering ...
    (microsoft.public.excel)
  • Re: Text formatted cells displaying numbers in scientific format
    ... It is just Excel trying to be helpful. ... When it 'sees' a large number it uses scientific format since ... I accept that Excel can't handle numbers greater than 15 digits and your ... string was 16 digits or more to be converted to a scientific value. ...
    (microsoft.public.excel)
  • Re: I hate dates
    ... the month as either a number or text string. ... the function requires the year to be four digits long. ... It is not advisable to do this (use two-digit years) as each computer can ... can then format anyway you want or need to using the Format function. ...
    (comp.lang.basic.visual.misc)
  • Re: I hate dates
    ... month as either a number or text string. ... the function requires the year to be four digits long. ... It is not advisable to do this (use two-digit years) as each computer can ... can then format anyway you want or need to using the Format function. ...
    (comp.lang.basic.visual.misc)
  • Re: DateTime.ParseExact weird behavior
    ... more inteligent behavior from the parser in this particular case. ... This is a string that is impossible to parse ... format to parse it. ... and as it can be either one or two digits it will read two ...
    (microsoft.public.dotnet.framework)