Smarty Forum Index Smarty
The discussions here are for Smarty, a template engine for the PHP programming language.

String to Float not work

 
Post new topic   Reply to topic    Smarty Forum Index -> General
View previous topic :: View next topic  
Author Message
donja
Smarty n00b


Joined: 31 Jul 2020
Posts: 1

PostPosted: Fri Jul 31, 2020 10:01 am    Post subject: String to Float not work Reply with quote

Hallo,

ich versuche folgende Rechnung:
Quote:
{math assign="test1" equation="x - y" x=$value1 y=$value2 format="%.2f"}
Ergebnis: {$test1}


Anbei mein Rangehen:

Quote:
{assign var="myArray" value="€"|explode:$cart_total}
{assign var=warenkorbpreis value=$myArray[0]|replace:",":"."}


Ergibt (zum Beispiel): 49.86

Quote:
{assign var=versandfreikosten value=TXT_BEISPIEL}


Ergibt (zum Beispiel): 89


Beide Werte werden ausgegeben aber nicht berechnet.
Nun teste ich den Variablentyp um evtl. zu konvertieren.

Quote:

{if $versandfreikosten|is_string}
{$value1 = $versandfreikosten|floatval}
{if $value1|is_float} float
{$value1}
{/if}
{/if}

Es wird mir 89 ausgegeben.

Quote:

{if $warenkorbpreis|is_string}
{$value2 = $warenkorbpreis|floatval}
{if $value2|is_float} float
{$value2}
{/if}
{/if}

Hier wird mir 0 ausgegeben.

Könnt Ihr mir helfen woran es liegt?

Viele Grüße

Donja



----------------------------------------
Try in Englisch:

helllo,

ich try to calc:
Quote:
{math assign="test1" equation="x - y" x=$value1 y=$value2 format="%.2f"}
result: {$test1}


this is how i will like to get my variables for the calculation:

Quote:

{assign var="myArray" value="€"|explode:$cart_total}
{assign var=warenkorbpreis value=$myArray[0]|replace:",":"."}


Result (for example): 49.86

Quote:
{assign var=versandfreikosten value=TXT_BEISPIEL}


Result (for example): 89


both variables will be show but not calculate. so i check wicht type of variable it is and convert to float for the calculation.

Quote:

{if $versandfreikosten|is_string}
{$value1 = $versandfreikosten|floatval}
{if $value1|is_float} float
{$value1}
{/if}
{/if}


result is 89

Quote:

{if $warenkorbpreis|is_string}
{$value2 = $warenkorbpreis|floatval}
{if $value2|is_float} float
{$value2}
{/if}
{/if}


result is 0

can you help, why the result is 0 and not 49.86?

yours

Donja
Back to top
View user's profile Send private message
AnrDaemon
Administrator


Joined: 03 Dec 2012
Posts: 1633

PostPosted: Sat Aug 01, 2020 12:09 pm    Post subject: Reply with quote

Try this code:

Code:
{$test1=($value1 - $value2)}
{$test1|number_format:",":" "}
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Smarty Forum Index -> General All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group
Protected by Anti-Spam ACP