From: Webbiz on
On Wed, 02 Dec 2009 09:40:44 +0000, Dee Earley
<dee.earley(a)icode.co.uk> wrote:

>On 01/12/2009 20:07, Webbiz wrote:
>> On Tue, 01 Dec 2009 08:53:00 +0000, Dee Earley
>> <dee.earley(a)icode.co.uk> wrote:
>>
>>> On 01/12/2009 06:48, Webbiz wrote:
>>>> On Mon, 30 Nov 2009 21:21:52 -0500, "MikeD"<nobody(a)nowhere.edu> wrote:
>>>>> "Webbiz"<nospam(a)noway.com> wrote
>>>>>> Private Function DisplayAvgATR(ByRef Ranges() As Single) As String
>>>>
>>>> BTW, while you've no problem 'nitpicking'<g>, got any suggestions on
>>>> a snazy way to set the format pattern of the "#.####" based on the way
>>>> the values are formatted in the array to begin with?
>>>
>>> The values in the array wont have a format as they are Singles.
>>> Formatting only applies when you convert to a string for display purposes.
>>> If you want the caller to be able to control the output format, either
>>> return a single and let it format it or take the format string as a
>>> parameter.
>>
>>
>> I'm sorry, I didn't explain myself well.
>>
>> The data in the array is of a particular format although Singles.
>> Stock and Commodity prices come in various formats. Here are some
>> common ones:
>>
><SNIP>
>>
>> Actual prices at the time of writing this. :-)
>>
>> So when one of these data files are loaded in, the output displays
>> would need to display results that are also in the appropriate format.
>
>But the input values do not have a format by definition of being a
>Single value.
>Yes, they will have some amount of decimal places, but no specific
>"000.00000" format so in your example, the code will ONLY see:
>
>82.625 Live Cattle
>59.2 Lean Hogs
>
>399.75 Corn
>1059.5 Soybeans
>562.5 Wheat
>
>If your calling code wants a specific format, it should format the value
>it gets back (which will also be unformatted)
>It is still much safer to pass numbers around as numbers rather than
>formatted strings, then only convert them for display.
>
>Another example:
>a=1.4444444:?a
> 1.4444444
>b=2.0000000:?b
> 2
>c=2.5555556:?c
> 2.5555556
>d=3.0000000:?d
> 3
>?(a+b+c+d)/4
> 2.25
>
>You can possibly determine the number of decimal places a number has by
>multiplying by 10 in a loop until it approaches 0 (Don't use exactly 0
>due to floating point maths)


What I ended up doing was to insert this code at the point where
prices are extracted by database and stored into a data array.

If DigitsRightOfDec(CStr(Samples(nArrCnt).High)) > nMaxNum Then
nMaxNum = DigitsRightOfDec(CStr(Samples(nArrCnt).High))
End If

Whatever the maximum number of digits are right of the decimal, that
value is then used to create the format for all price displays for
that particular data set loaded.

Public Sub SetDataFormat(ByVal Digits As Long)

sDataFormat = "#"

If Digits > 0 Then sDataFormat = sDataFormat & "." &
Right("000000000", Digits)

End Sub

This sDataFormat is then used for all the Format() displays of the
data.

Seems to be working.

Thanks.

Webbiz


From: Webbiz on
On Wed, 02 Dec 2009 20:28:16 GMT, deactivated wrote:

>On Tue, 01 Dec 2009 00:48:11 -0600, Webbiz <nospam(a)noway.com> wrote:
>
>>On Mon, 30 Nov 2009 21:21:52 -0500, "MikeD" <nobody(a)nowhere.edu>
>>wrote:
>
>>>Private Function CalcAvg(ByRef Ranges() As Single) As String
>>>Dim n As Long
>>>Dim sngTotal As Single
>>>Dim sngAvg As Single
>>>
>>> For n = LBound(Ranges) To UBound(Ranges)
>>> sngTotal = sngTotal + Ranges(n)
>>> Next n
>>>
>>>
>>>
>>> 'Return the Average
>>> sngAvg = sngTotal / (UBound(Ranges) - LBound(Ranges) + 1)
>>> CalcAvg = Format$(sngAvg, "#.######")
>>>
>>>End Function
>
>Should not a function do a single thing?
>
>Like calculate an average and return in the same type.
>
>Any user who wants to format the result can then put it in another
>function that would format the string.
>
>That way the user can reuse that average function elsewhere as it is
>almost generic.


I see your point.

What I've noticed in all the time I've been learning things from this
newsgroup is that the mindset is geared towards programming with the
idea that others may be reusing the code or needing to work with it.

Why my code tends not to usually come out this way is that I'm a lone
soul in a home office writing code that only I will ever use.

But I agree that my mindset needs to be adjusted towards a more
reusable and cooperative mentality. And so I appreciate these
reminders.

:-)

Webbiz
From: Nobody on
"Webbiz" <nospam(a)noway.com> wrote in message
news:3noih59pnegc5oa59j5h2346o9ehbi08k2(a)4ax.com...
> What I ended up doing was to insert this code at the point where
> prices are extracted by database and stored into a data array.
>
> If DigitsRightOfDec(CStr(Samples(nArrCnt).High)) > nMaxNum Then
> nMaxNum = DigitsRightOfDec(CStr(Samples(nArrCnt).High))
> End If

Avoid using CStr() in this case. I think DigitsRightOfDec() looks for "."
and CStr() would use the decimal separator in Control Panel, which could be
"," or any other character. Use Str() instead, which always use US-English.

The same applies when using Val(). It recognizes only US-English, so use
Str/Val combo, or CStr/SSng or CStr/CDbl() to translate back and forth, and
when using Cxxx functions, make sure that the locale is still the same. If
you intend on making users from different locales send data to each other,
store the data in its binary form, or use Str/Val combo which uses fixed
format to avoid locale issues.



From: Webbiz on
On Fri, 4 Dec 2009 14:59:23 -0500, "Nobody" <nobody(a)nobody.com> wrote:

>"Webbiz" <nospam(a)noway.com> wrote in message
>news:3noih59pnegc5oa59j5h2346o9ehbi08k2(a)4ax.com...
>> What I ended up doing was to insert this code at the point where
>> prices are extracted by database and stored into a data array.
>>
>> If DigitsRightOfDec(CStr(Samples(nArrCnt).High)) > nMaxNum Then
>> nMaxNum = DigitsRightOfDec(CStr(Samples(nArrCnt).High))
>> End If
>
>Avoid using CStr() in this case. I think DigitsRightOfDec() looks for "."
>and CStr() would use the decimal separator in Control Panel, which could be
>"," or any other character. Use Str() instead, which always use US-English.
>
>The same applies when using Val(). It recognizes only US-English, so use
>Str/Val combo, or CStr/SSng or CStr/CDbl() to translate back and forth, and
>when using Cxxx functions, make sure that the locale is still the same. If
>you intend on making users from different locales send data to each other,
>store the data in its binary form, or use Str/Val combo which uses fixed
>format to avoid locale issues.
>
>


Interesting point I didn't think of. Now I would have to be concerned
whether the data being loaded itself would have a "," rather than a
"."

Hmmm.

thanks.

Webbiz