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
Related
I'm trying to convert a string number like 14,767 or 96,812.10 or 6,780,766.50 but if I use
(double)$fildOfDataBase or (float)$fildOfDataBase this put a integer format as 14 o 96 and I don't know why, so my question is how can i convert a string to a number format, hope some one can help me I'm using laravel 8
You need to remove the thousands separator first
(float)str_replace(',', '', $fildOfDataBase);
Another more "strict" way to do it is to use numfmt_parse
//in your case use the en_EN format
$fmt = numfmt_create( 'en_EN', NumberFormatter::DECIMAL );
numfmt_parse($fmt, $fildOfDataBase);
I am using contact form 7 and want to disallow users to enter repeated numbers like 1111111 or 2222222 in phone field.
I am using below code to enter only 10 digits. Can anyone help me with what I should change or add in this to work.
// define the wpcf7_is_tel callback<br>
function custom_filter_wpcf7_is_tel( $result, $tel ) {<br>
$result = preg_match( '/^\(?\+?([0-9]{0})?\)?[-\. ]?(\d{10})$/', $tel );<br>
return $result; <br>
}<br>
add_filter( 'wpcf7_is_tel', 'custom_filter_wpcf7_is_tel', 10, 2 );
First of all, [0-9]{0} looks like a typo as this pattern matches nothing, an empty string. You probably wanted to match an optional area code, three digits. So, use \d{3} if you meant that.
Next, to disallow same digits within those you match with \d{10}, you simply need to re-write it as (?!(\d)\1{9})\d{10}.
Bearing in mind what was said above, the solution is
function custom_filter_wpcf7_is_tel( $result, $tel ) {<br>
return preg_match('/^\(?\+?(?:\d{3})?\)?[-. ]?(?!(\d)\1{9})\d{10}$/', $tel );<br>
}
See the regex demo.
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:",":"."}
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
I want to put letters instead of numbers.For example, if I have the following statement:
{for $node=1 to {$nr_nods}}
{$nod}<br>
{/for}
where {$nr_nods}=3, will show
1
2
3
,but Y want display
A
B
C
how make this?
In php, assign an array to the template with the equivalences:
$smarty->assign('nums'=>array(1=>'A',2=>'B',3=>'C'));
and then just output the values by key:
{$nums.$nod}