php NumberFormatter format to spelling [duplicate] - php

This question already has answers here:
Number to words in php using NumberFormatter class
(2 answers)
Closed 2 years ago.
hi im using php NumberFormatter in laravel
now if i want to echo number i did this code
#php
$digit = new \NumberFormatter(Lang::locale(), \NumberFormatter::SPELLOUT);
echo $digit->format(round(10.557,3));
#endphp
the result will be
but i need it to be
ten and five handred and seventy five
how can i change the format ..
thanks ..

you can try to build a function manually
function numberToText($number) {
$digit = new \NumberFormatter(Lang::locale(), \NumberFormatter::SPELLOUT);
$numberParts = explode('.', (string) round($number,3));
$formatedNumber = $digit->format($numberParts[0]);
if (isset($numberParts[1] ) {
$formatedNumber .= ' and ' . $digit->format($numberParts[1]);
}
return $formatedNumber;
}
#php
echo numberToText(10.557);
#endphp

Related

Why does the Code only show 'array' [duplicate]

This question already has answers here:
Why does this PHP code just echo "Array"?
(6 answers)
How can I echo or print an array in PHP?
(14 answers)
Closed 4 years ago.
Hello Guys I try to short the content of a post in a preview page. I have tried out the code and it doesn't work. It shows only an 'array' as text. Is this echo wrong or any other failures?
$word=Hello World;
$words = str_word_count($word, 1,'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ');
$len = min(50, count($words));
$first_fifty = array_slice($words, 0, $len);
echo $first_fifty;
Thanks for Help...
There are 2 mistakes:
$word=Hello World; should be $word='Hello World';
You are echoing an array; do print_r ($first_fifty); instead;
All the code:
<?php
$word='Hello World';
$words = str_word_count($word, 1,'ŠŒŽšœžŸ¥µÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝßàáâãäåæçèéêëìíîïðñòóôõöøùúûüýÿ');
$len = min(50, count($words));
$first_fifty = array_slice($words, 0, $len);
print_r ($first_fifty);
?>
You can echo:
<?php
echo $first_fifty[0] . ' ' . $first_fifty[1];
?>

Removing trailing zero's from decimal [duplicate]

This question already has answers here:
Show a number to two decimal places
(25 answers)
Closed 8 years ago.
Hi i was wondering how i could remove the last 2 zero's as seen below:
13.6500
16.0000
17.5345
To the following:
13.65
16.00
17.5345
You can use round() like this to round to 2 decimal points like this
echo round("13.6500",2);
Also you can use number_format() like this
echo number_format("13.6500", 2);
Both yeilds the O/P
13.65
Demo
EDIT
from the comment i think what you want is like this
$input = "12.00000";
$length = strlen(substr(strrchr($input, "."), 1));
if($length > 2)
{
$input =$input+0;
if(strlen(substr(strrchr($input, "."), 1)) < 1)
{
$input = $input.".00";
}
else if(strlen(substr(strrchr($input, "."), 1)) < 2)
{
$input = $input.".0";
}
echo $input;
}
else
{
echo $input;
}
Demo

PHP output string with variable? [duplicate]

This question already has answers here:
PHP is confused when adding and concatenating
(3 answers)
Closed 8 years ago.
I don't understand why it output -2yyy !
How can I output xxx8yyy ?
$ten = 10;
$two = 2;
echo "xxx".$ten - $two."yyy"; //-2yyy
echo "xxx".($ten - $two)."yyy";
Try this.
Just add brackets:
<?php
$ten = 10;
$two = 2;
echo "xxx".($ten - $two)."yyy"; // xxx8yyy

generating a unique no for coupon code 6 to 8 digit for any no of coupon [duplicate]

This question already has answers here:
Generating unique 6 digit code
(6 answers)
Closed 8 years ago.
for($i=0,$ii=1;$i<$_POST['no_of_coupon']; $ii++) {
$unique_code=uniqid();
$category_unique_code = substr($unique_code,rand(0,strlen($unique_code) - 6),6);
$i++;
echo $category_unique_code;
}
$_POST['no_of_coupon'] is the no . For eg. if user want 1 lakh coupon code or more i.e. $_POST['no_of_coupon'], all the code is inserted into the the database and the code is unique but I have tried above method , but its not unique then i tried another method
function gen_random($length=32)
{
$final_rand='';
for($i=0;$i< $length;$i++)
{
$final_rand .= rand(0,9);
}
return $final_rand;
}
for($i=0,$ii=1;$i<$_POST['no_of_coupon']; $ii++) {
$unique_code=gen_random(6);
$category_unique_code = substr($unique_code,rand(0,strlen($unique_code) - 6),6);
$i++;
echo $category_unique_code;
}
This method is also not generating unique coupon code , I just need 6 to 8 digit unique no So somebody have any idea to generate unique no , please tell me
This code will generate an 8 digit unique coupon code:
function getUniqueCouponCode ()
{
$filename = 'number.txt';
if (file_exists($filename)) {
$actual_number = file_get_contents($filename);
} else {
$actual_number = 1;
}
file_put_contents($filename, $actual_number + 1);
return str_pad($actual_number, 8, '0', STR_PAD_LEFT);
}
Try this to create a 6 or 8 digit unique no:
<?php
$date=date('y-m-dh:i:s');
echo substr(md5($date),0,6);
?>

php: best way to assign 10 times the same symbol (space) to a variable [duplicate]

This question already has answers here:
make string of N characters
(5 answers)
Closed 10 years ago.
We have
$symb="_";
$num=10;
We want $ten_symbs to be exactly "__________"; // ten symbols "_".
Whats the fastest and/or the best way to assign ten "_" to $ten_symbs?
str_repeat():
$symbol = '_';
$num = 10;
echo str_repeat($symbol, $num);
You can use str_repeat as:
$ten_symbs = str_repeat($symb, $num);
You can also do:
$ten_symbs = str_pad('',$num,$symb);
But the fist option is cleaner.
look for str_repeat() here: http://www.php.net/manual/de/function.str-repeat.php
$ten_symbs = '';
for($i=0;$i<$num;$i++) {
$ten_symbs .= $symb;
}

Categories