Smarty template with comma and decimal - smarty

I have to calculate a smarty variable with 23,50 Euro e.g if variable = 100 than {$variable+23,50} it should be 123,50 Euro. In template the variable accepts only {$variable+23.50} which gives me 24,50. the same with 1000 Euro {$variable+23,50} I need a value 1.023,50
can someone help me please.
thanks in advance
I have tried replace, string_format etc. but nothing worked.

You have to use the {math} from Smarty, like this :
{math equation="x + y" x=$variable y=23.50 format="%.2f"}
If you want to replace comma by a point, use math replace :
{math|replace:",":"."}

Related

Laravel calculator can't give 8 numbers after the dot

I have create a simple price calculator in laravel. I have some problem when the price goes below the 0.00
Result of calculator must be:
0.00000005
But now its giving:
5.0E-8
Can anyone help me to get the result with 8 digit?
Use printf with the appropriate format specifier:
printf("%.8f", 5 / 1e8);
You can use number_format() or sprintf() to get desired output.
Note that output will be displayed as string.
sprintf('%.8f', $val)
// ex. 0.00000005
number_format($val, 8)
// ex. 0.00000005

FoxPro convert currency to numeric

I'm using Visual FoxPro and I need to convert currency amount into numeric. The 2 columns in the table are tranamt(numeric,12,2) and tranamt2(character)
Here's my example:
tranamt2=-$710,000.99
I've tried
replace all tranamt with val(tranamt2)
and
replace all tranamt with val(strtran(tranamt2, ",",""))
both results give me zero. I know it has something to do with the negative sign but I can't figure it out. Any help is appreciated.
Try this:
replace all tranamt with VAL(STRTRAN(STRTRAN(tranamt2, "$", ""), ",", ""))
This removes the dollar sign and comma in one shot.
need to convert currency amount into numeric
tranamt(numeric,12,2) and tranamt2(character)
First of all a neither a Character Field Type nor a Numeric Field type (tranamt2) are Not a VFP Currency Field type
You may be using the value of a Character field to represent currency, but that does not make it a currency value - just a String value.
And typically when that is done, you do NOT store the Dollar Sign '$' and Comma ',' as part of the data.
Instead, you store the 'raw' value (in this case: "-710000.99") and merely format how that 'raw' value is displayed when needed.
So in your Character field you have a value of: -$710,000.99
Do you have the Dollar Sign '$' and the Comma ',' as part of the field data?
If so, to convert it to a Numeric, you will first have to eliminate those extraneous characters prior to the the conversion.
If they are not stored as part of your field value, then you can use the VAL() 'as is'.
Example:
cStr = "-710000.99" && The '$' and ',' are NOT stored as part of Character value
nStr = VAL(cStr)
?nStr
However if you have the Dollar Sign and the Comma as part of the field data itself, then you can use STRTRAN() to eliminate them during the conversion.
Example:
cStr = "-$710,000.99" && Note both '$' and ',' are part of data value
* --- Remove both '$' and ',' and convert with VAL() ---
nStr = VAL(STRTRAN(STRTRAN(cStr,",",""),"$",""))
?nStr
Maybe something like:
REPLACE tranamt WITH VAL(STRTRAN(STRTRAN(tranamt2,",",""),"$",""))
EDIT: Another alternative would be to use CHRTRAN() to remove the '$' and ','
Something like:
cRemoveChar = "$," && Characters to be removed from String
REPLACE tranamt WITH VAL(CHRTRAN(tranamt2,cRemoveChar,""))
Good Luck
A little late but I use this function call
function MoneyToDecimal
LPARAMETER tnAmount
LOCAL lnAmount
IF VARTYPE(tnAmount) = "Y"
lnAmount = VAL(STRTRAN(TRANSFORM(tnAmount), "$", ""))
ELSE
lnAmount = tnAmount
ENDIF
return lnAmount
endfunc
And can be tested with these calls:
wait wind MoneyToDecimal($112.50)
wait wind MoneyToDecimal($-112.50)
Use the built-in MTON() function to convert a currency value into a numeric value:
replace all tranamt with mton(tranamt2)

In angular 2, how to display a number as two decimal rounded currency?

Examples:
1.99 --> $1.99
1.9 --> $1.90
1 --> $1.00
1.005 --> $1.01
1.004 --> $1.00
I am using {{num | currency:'USD':true}} but it does not show trailing 0s.
Use this code. Here is a working example http://plnkr.co/edit/xnN1HnJtTel6WA24GttR?p=preview
{{num | currency:'USD':true:'1.2-2'}}
Explanation :
number_expression | number[:digitInfo]
Finally we get a decimal number as text. Find the description.
number_expression: An angular expression that will give output a number.
number : A pipe keyword that is used with pipe operator.
digitInfo : It defines number format.
Now we will understand how to use digitInfo. The syntax for digitInfo is as follows.
{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}
Find the description.
minIntegerDigits : Minimum number of integer digits. Default is 1. (in our case 1)
minFractionDigits : Minimum number of fraction digits. Default is 0. (in our case 2)
maxFractionDigits : Maximum number of fraction digits. Default is 3. (in our case 2)
well you got the correct answer but still i think i can elaborate more this answer so posting it as answer:
First of all there are number of pipes available of the angular2 to use in our project some of them are listed below
CurrencyPipe , DatePipe, UpperCasePipe, LowerCasePipe, and PercentPipe and many more.
Here as your question you have problem related to currency pipe. so i want to explain bit more as other answers.
CurrencyPipe :
A pipe may accept any number of optional parameters to fine-tune its output. We add parameters to a pipe by following the pipe name with a colon ( : ) and then the parameter value (e.g., currency:'EUR'). If our pipe accepts multiple parameters, we separate the values with colons (e.g. slice:1:5).
{{number | currency:'your_type':true:'1.2-2'}}
here...first parameter is currency type which is either EUR,USD or anything, Second parameter is true/false for the symbolDisplay which is false byDefault. then Third one is range limit basically a range limit . You can set a min and max length after the decimal point and a fixed number (or leave it blank for default) for the places before the decimal point.
I have found some good tutorials for the pipes in the angular2 which i am posting here..
http://voidcanvas.com/angular-2-pipes-filters/
https://angular.io/docs/ts/latest/guide/pipes.html
Hope it Helps and clarify more about pipes !!
#Pardeep !!
You are using the correct pipe. You just need to add the digit info to the output.
{{num | currency:'USD':true}} should be...
{{num | currency:'USD':true:'1.2-2'}}
For reference: 'USD' represents the type of currency, true represents whether to show the currency symbol ($), and '1.2-2' represents the digit info.
The digit info is {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}.
minIntegerDigits is the minimum number of integer digits to use and defaults to 1.
minFractionDigits is the minimum number of digits after fraction and defaults to 0.
maxFractionDigits is the maximum number of digits after fraction and defaults to 3.
Source for the digit info can be found here: https://angular.io/docs/ts/latest/api/common/index/DecimalPipe-pipe.html
If, like me, you're trying to do this in the typescript/javascript rather than HTML, you can also use toLocaleString
So to convert a number to a currency string:
ppCurrency(number) {
return number.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
}
Following will convert with 2 decimal digits
{{num | currency : '$' : 2}}
angular 2
{{num | currency:'USD':true:'1.2-2'}}
<input type="number" [(ngModel)]="myModel" (keyup)="onBlurMethod()">
<br>
<br> The formatted currency is :
<br> {{myModel | currency:'USD':true:'1.2-2' }}
Here is the working example.
http://plnkr.co/edit/pSK8p7u3oo4WsAB9aFBS?p=preview
If any body gets a warning and wanted to resolve it, please find below the fix I used.
Following is the warning Angular displays in browser console:
Warning: the currency pipe has been changed in Angular v5. The
symbolDisplay option (third parameter) is now a string instead of a
boolean. The accepted values are "code", "symbol" or "symbol-narrow".
The format that causes the warning: currency: "USD":true:"1.2-2"
Fix: currency: "USD":'symbol':"1.2-2"
Reference: https://angular.io/api/common/CurrencyPipe

Ruby - conversion string to float return 0.0

In a variable is stored this value: $10.00
And I need to get this 10.00
I've tried to convert this value to float:
new_price = '%.2f' % (price.to_f)
but I get just 0.0.
What's wrong with that?
I've tried also
price = price.strip
price[0]=""
new_price = '%.2f' % (price.to_f)
But even this didn't help me... where is a problem?
Thanks
You need to remove the $ first. The whole thing like this:
'%.2f' % '$10.00'.delete( "$" ).to_f
or
'%.2f' % '$10.00'[1..-1].to_f
if you like density and may encounter non dollars.
To set it in a variable:
current_price= '%.2f' % '$10.00'.delete( "$" ).to_f
The more common error, is a value in the thousands where there's a comma in the string like: 10,000.00. The comma will cause the same truncation error, but the decimal won't, and many programmers won't even catch it (we don't even notice the comma anymore). To fix that:
current_price= '%.2f' % '10,000.00'.delete( "," ).to_f
Adding on to froderick's answer of:
You need to remove the $ first. The whole thing like this:
'%.2f' % '$10.00'.delete( "$" ).to_f
or
'%.2f' % '$10.00'[1..-1].to_f
if you like density and may encounter non dollars.
you need to format the code for output to ensure that you get two decimal places >with
You need to format your output string to ensure you get two decimal places.
puts "Current amount: #{format("%.2f", amount)}"

String value formatting

Sometimes in Strings I see something like this %1$s or this %2$d. Can somebody explain to me how to read such things?
Check this document http://download.oracle.com/javase/1,5.0/docs/api/java/util/Formatter.html#syntax the string is basically broken in
%[argument_index$][flags][width][.precision]conversion
From your example %1$s,
% means replace with a parameter
1$ is the position in the parameter array.
s signals that the parameter is a string.
This is taken from Java, but a lot of programming languages use the same syntax for string formatting.
the % stand for the relative argument position, and the "s" or "d" (or others) stands for the type.
This is used to format a string through the printf functions
format= 'The %2$s contains %1$04d monkeys';
printf(format, num, location);
see the printf docs of the langage you use to get all the details (there is a lot)

Resources