I don't think it's a good idea to have StrD() to output a max of 10 digits by default after the decimal point.help wrote: A maximum number of decimal places may be specified, if not it will be set to 10 decimal places, with removing the trailing zeros.
I would expect a double -> string conversion to keep all the available digits, and to discard them only when I ask for it explicitly specifying the second parameter.
Code: Select all
dd.d = 1.0/3.0
Debug dd ; 0.33333333333333331
Debug StrD(dd) ; 0.3333333333
; why ? no param specified, I just want the same number above converted to string
Debug StrD(dd, 3) ; 0.333
; now that's ok, I'm asking for this to happen
Code: Select all
dd.d = 1.234
Debug StrD(dd) ; 10 digits is the default
; result is 1.234
Debug StrD(dd, 10) ; same as the default and I get a different output... doesn't make a lot of sense
; result is 1.2340000000
So, in my opinion: StrD() without the optional param should convert all the available digits to the string representation.
Code: Select all
Procedure.s MyStrD (value.d, digits = #PB_Ignore)
Protected Ret$
Protected *in.Character, i
If digits = #PB_Ignore
Ret$ = ReverseString(StrD(value, 32))
*in = @Ret$
While *in\c = '0'
i + 1
*in + SizeOf(Character)
Wend
If *in\c = '.'
i + 1
EndIf
Ret$ = ReverseString(Mid(Ret$, i+1))
Else
Ret$ = StrD(value, digits)
EndIf
ProcedureReturn Ret$
EndProcedure
; FULL DIGITS FOR THE DEFAULT SEEMS PREFERABLE TO ME
Debug MyStrD(123.5e-20) + " vs " + StrD(123.5e-20)
; 0.000000000000000001235 vs 0
Debug MyStrD(1.234025025075) + " vs " + StrD(1.234025025075)
; 1.234025025075 vs 1.2340250251
Debug MyStrD(10) + " vs " + StrD(10)
; 10 vs 10
Debug MyStrD(10.5) + " vs " + StrD(10.5)
; 10.5 vs 10.5
Debug MyStrD(1.0/3.0) + " vs " + StrD(1.0/3.0)
; 0.33333333333333331 vs 0.3333333333
; EXAMPLE
dd.d = 0.00045012052407 ; how can I print the unknown number in dd ?
Debug StrD(dd) ; the default ? maybe I'm loosing some digits... -> 0.0004501205
Debug StrD(dd, 30) ; this way ? mabye I'm printing too many unneeded zeros ... -> 0.000450120524070000000000000000
Debug MyStrD(dd) ; finally, the natural way -> 0.00045012052407