Optional DecimalPoint$ for StrF() and StrD()

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

Optional DecimalPoint$ for StrF() and StrD()

Post 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 "."
Last edited by Lebostein on Wed Jan 25, 2017 2:26 pm, edited 2 times in total.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

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

Post by Dude »

Lebostein wrote:Result.f = ValF(String$ , DecimalPoint$])
That's why. :)
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

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

Post 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
User avatar
Michael Vogel
Addict
Addict
Posts: 2797
Joined: Thu Feb 09, 2006 11:27 pm
Contact:

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

Post 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,",")
Lebostein
Addict
Addict
Posts: 826
Joined: Fri Jun 11, 2004 7:07 am

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

Post 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.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

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

Post 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.
User avatar
nco2k
Addict
Addict
Posts: 1344
Joined: Mon Sep 15, 2003 5:55 am

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

Post 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
If OSVersion() = #PB_OS_Windows_ME : End : EndIf
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

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

Post 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.
Post Reply