Page 1 of 1

Optional DecimalPoint$ for StrF() and StrD()

Posted: Wed Jan 25, 2017 10:30 am
by Lebostein
With the new FormatNumber() function in PB 5.50, PureBasic introduces a configurable decimal point character.
For the consistency: Why these optional DecimalPoint$ parameter is not added to the Str*() functions?

Result$ = FormatNumber(Number.d [, NbDecimals [, DecimalPoint$ [, ThousandSeperator$]]])
Result$ = StrF(Value.f [, NbDecimal [, DecimalPoint$]])
Result$ = StrD(Value.d [, NbDecimal [, DecimalPoint$]])

Would be nice if the current DecimalPoint$ could be interpreted also by the ValF() and ValD() function...

Result.f = ValF(String$ [, DecimalPoint$])
Result.d = ValD(String$ [, DecimalPoint$])

Alternative:
A function like SetDecimalPoint(DecimalPoint$) to set the decimal point character for all conversion functions (FormatNumber, StrF, StrD, ValF, ValD) globally. Default value is "."

Re: Optional DecimalPoint$ for StrF() and StrD()

Posted: Wed Jan 25, 2017 1:12 pm
by Dude
Lebostein wrote:Result.f = ValF(String$ , DecimalPoint$])
That's why. :)

Re: Optional DecimalPoint$ for StrF() and StrD()

Posted: Wed Jan 25, 2017 2:07 pm
by Lebostein
Dude wrote:
Lebostein wrote:Result.f = ValF(String$ , DecimalPoint$])
That's why. :)
What's the point? Sure you understood the problem?

12.35 = ValF("12.35")
12.35 = ValF("12,35", ",")

https://en.wikipedia.org/wiki/Decimal_m ... imal_point
https://en.wikipedia.org/wiki/Decimal_m ... imal_comma

Re: Optional DecimalPoint$ for StrF() and StrD()

Posted: Wed Jan 25, 2017 2:37 pm
by Michael Vogel
Wouldn't it make everything a little bit slower?
How to handle the case StrF(123.456,",") where you don't use the decimals parameter?
And what about the group delimiter (for thousands)?
And signs, exponents, currency symbols etc.?

Would just write my own routines depending on what is needed...

Code: Select all

Procedure.s MyStrF(value.f,decimals=-1,point.s=".")
	
	If decimals<0
		ProcedureReturn ReplaceString(StrF(value),".",point)
	Else
		ProcedureReturn ReplaceString(StrF(value,decimals),".",point)
	EndIf
		
EndProcedure
Macro MyValF(value,point=".")
	
	ReplaceString(value,".",point,#PB_String_InPlace)
		
EndMacro

s.s=StrF(#PI)
t.s=MyStrF(#PI,5,",")

Debug StrF(#PI)
Debug MyStrF(#PI)
Debug MyStrF(#PI,2)
Debug MyStrF(#PI,5,",")

Debug ""

Debug ValF(s)
Debug MyValF(s)
Debug MyValF(t)
Debug MyValF(t,",")

Re: Optional DecimalPoint$ for StrF() and StrD()

Posted: Wed Jan 25, 2017 8:47 pm
by Lebostein
:cry: I need no workaround with ReplaceString, but Thanks.

I am here for the consistency. If Fred introduces a decimal point character then he should add this parameter to all conversion functions.

Re: Optional DecimalPoint$ for StrF() and StrD()

Posted: Thu Jan 26, 2017 12:36 am
by Dude
Lebostein wrote:What's the point?
How can a float (number) also be a string? That's what you're requesting. It's impossible.

Re: Optional DecimalPoint$ for StrF() and StrD()

Posted: Thu Jan 26, 2017 1:28 am
by nco2k
thats obviously not what he was requesting:

Result.f = ValF("1.23"); = 1.23
Result.f = ValF("1.23", "."); = 1.23
Result.f = ValF("1,23", ","); = 1.23

c ya,
nco2k

Re: Optional DecimalPoint$ for StrF() and StrD()

Posted: Thu Jan 26, 2017 2:39 am
by Dude
@nco2k, I understand now what Lebostein means.

But in his first post, he posted this as an example:

Code: Select all

Result.f = ValF(String$ [, DecimalPoint$])
Which was not obvious at all that he wanted to replace something in String$ with DecimalPoint$. It looked like he wanted to replace the result with a different decimal character, instead of formatting String$ to strip it first. You can interpret it either way. :P

Anyway, I get it now. Sorry for my misunderstanding.