Understanding numberformat

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Understanding numberformat

Mike Kerner via use-livecode
Hi friends,

maybe I have a little understanding problem with numberformat.
Looks like this is "temporary/fleeting"?

Meanwhile I worked around this with the FORMAT() function,
but would like to understand the problem.

I have this button script:
###############################################################
## Neccessary function, since in most non-enlsih speaking countries like germany
## we use the COMMA as a decimal delimiter
## Neccessary for calculations
## tZahl = tNumber
## ohne_komma = without comma
function ohne_komma tZahl
  replace "," with "." in tZahl
  return tZahl
end ohne_komma

## mit_komma = with comma
## Neccessary for displaying numbers
function mit_komma tZahl
   replace "." with "," in tZahl
   return tZahl
end mit_komma

on mouseUp pMouseButton
   put 10  into menge ## amount
   put "2,3" into preis ## price
   
   set the numberformat to 0.00
   put ohne_komma(menge) into menge
   put ohne_komma(preis) into preis
   put  menge * preis into ergebnis ## result

##!!!
   answer ergebnis ## -> 23.00
   answer mit_komma(ergebnis) ## -> 23
##!!!

end mouseUp
###############################################################

The last 2 lines of the mouseup handler are puzzling me, what do I not get?
Thanks for any insight!

It would be nice if LC would take these country specific differences into account
in one of the next versions.


Best

Klaus

--
Klaus Major
http://www.major-k.de
[hidden email]


_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Understanding numberformat

Mike Kerner via use-livecode
On 2018-04-06 13:17, Klaus major-k via use-livecode wrote:

> Hi friends,
>
> maybe I have a little understanding problem with numberformat.
> Looks like this is "temporary/fleeting"?
>
> Meanwhile I worked around this with the FORMAT() function,
> but would like to understand the problem.
>
> I have this button script:
> ###############################################################
> ## Neccessary function, since in most non-enlsih speaking countries
> like germany
> ## we use the COMMA as a decimal delimiter
> ## Neccessary for calculations
> ## tZahl = tNumber
> ## ohne_komma = without comma
> function ohne_komma tZahl
>   replace "," with "." in tZahl
>   return tZahl
> end ohne_komma
>
> ## mit_komma = with comma
> ## Neccessary for displaying numbers
> function mit_komma tZahl
>    replace "." with "," in tZahl
>    return tZahl
> end mit_komma
>
> on mouseUp pMouseButton
>    put 10  into menge ## amount
>    put "2,3" into preis ## price
>
>    set the numberformat to 0.00
>    put ohne_komma(menge) into menge
>    put ohne_komma(preis) into preis
>    put  menge * preis into ergebnis ## result
>
> ##!!!
>    answer ergebnis ## -> 23.00
>    answer mit_komma(ergebnis) ## -> 23
> ##!!!
>
> end mouseUp
> ###############################################################
>
> The last 2 lines of the mouseup handler are puzzling me, what do I not
> get?
> Thanks for any insight!

The numberFormat is a local property and is only used when a number gets
converted to a string...

put menge * preis into ergebnis -- ergebnis is a number
answer ergebnis -- ergebnis is converted to a string using the
numberFormat 0.00 and passed to 'answer'.

In the last line 'answer mit_komma(ergebnis)' you are passing ergebnis
(a number) to a function. In the function, that number is converted to a
string using the default numberFormat (as it is local to handlers, and
you have not set it) as you are using 'replace' on it.

So, the 'issue' here is the locality of numberFormat.

Warmest Regards,

Mark.

--
Mark Waddingham ~ [hidden email] ~ http://www.livecode.com/
LiveCode: Everyone can create apps

_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode
Reply | Threaded
Open this post in threaded view
|

Re: Understanding numberformat

Mike Kerner via use-livecode
Hi Mark,

> Am 06.04.2018 um 14:18 schrieb Mark Waddingham via use-livecode <[hidden email]>:
>
> On 2018-04-06 13:17, Klaus major-k via use-livecode wrote:
>> Hi friends,
>> maybe I have a little understanding problem with numberformat.
>> Looks like this is "temporary/fleeting"?
>> Meanwhile I worked around this with the FORMAT() function,
>> but would like to understand the problem.
>> I have this button script:
>> ###############################################################
>> ## Neccessary function, since in most non-enlsih speaking countries like germany
>> ## we use the COMMA as a decimal delimiter
>> ## Neccessary for calculations
>> ## tZahl = tNumber
>> ## ohne_komma = without comma
>> function ohne_komma tZahl
>>  replace "," with "." in tZahl
>>  return tZahl
>> end ohne_komma
>> ## mit_komma = with comma
>> ## Neccessary for displaying numbers
>> function mit_komma tZahl
>>   replace "." with "," in tZahl
>>   return tZahl
>> end mit_komma
>> on mouseUp pMouseButton
>>   put 10  into menge ## amount
>>   put "2,3" into preis ## price
>>   set the numberformat to 0.00
>>   put ohne_komma(menge) into menge
>>   put ohne_komma(preis) into preis
>>   put  menge * preis into ergebnis ## result
>> ##!!!
>>   answer ergebnis ## -> 23.00
>>   answer mit_komma(ergebnis) ## -> 23
>> ##!!!
>> end mouseUp
>> ###############################################################
>> The last 2 lines of the mouseup handler are puzzling me, what do I not get?
>> Thanks for any insight!
>
> The numberFormat is a local property and is only used when a number gets converted to a string...
>
> put menge * preis into ergebnis -- ergebnis is a number
> answer ergebnis -- ergebnis is converted to a string using the numberFormat 0.00 and passed to 'answer'.
>
> In the last line 'answer mit_komma(ergebnis)' you are passing ergebnis (a number) to a function. In the function, that number is converted to a string using the default numberFormat (as it is local to handlers, and you have not set it) as you are using 'replace' on it.
>
> So, the 'issue' here is the locality of numberFormat.
>
> Warmest Regards,
>
> Mark.

thank you very much for your explanation, I get it now! :-)


Best

Klaus

--
Klaus Major
http://www.major-k.de
[hidden email]
_______________________________________________
use-livecode mailing list
[hidden email]
Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
http://lists.runrev.com/mailman/listinfo/use-livecode