This question already has answers here:
PHP: Express Number in Words [duplicate]
(4 answers)
Closed 8 years ago.
Hi I was wondering how can I print out numbers in their word form ? When I did a Google search it showed other peoples script on how to print 234 as two hundred thirty four .
I need 234 as two three four.
Thank you guys!
I need 234 as two three four.
It would be as simple as creating an array of your number to word map, then taking your number and printing out the corresponding value:
<?php
$num_word = array();
$num_word[0] = 'zero';
$num_word[1] = 'one';
$num_word[2] = 'two'
...
$num_word[9] = 'nine';
$num = 234;
foreach(str_split($num) as $w) {
echo $num_word[$w];
}
?>
Related
This question already has answers here:
How to calculate correctly in php?
(6 answers)
Closed 1 year ago.
I am a newbie learning PHP and i am trying to find average of 3 numbers but not getting the correct answer. I don't know where i am going wrong.
function percentage($math,$eng,$sc){
$s = $math+$eng+$sc / 3 ;
return $s;
}
$p = percentage(10,20,30);
echo $p;
I am getting the ansewer as 40 whereas i am supposed to get 20. Kindly check if there is any error.
Return value is right. Check operators precedence.
If you want 20 as return value code is:
$s = ($math+$eng+$sc) / 3 ;
You forgot to use parentheses:
$s = ($math+$eng+$sc) / 3 ;
All things together:
function percentage($math,$eng,$sc){
$s = ($math+$eng+$sc) / 3 ;
return $s;
}
echo percentage(10,20,30);
This question already has answers here:
Add zero to before decimal point of a number
(2 answers)
Closed 2 years ago.
I have some data from database like this :
43966.31875
how to change 43966 to 0 and keep decimal number to 31875, What should I do?
well, for the final result like this :
0.31875
If my explanation is incomprehensible, I apologize, and you can ask me again, Thank You
The regex method:
$string = '43966.31875';
echo preg_replace('/(\d+).(\d+)/i', '0.${2}', $string);
Will split number into 2 parts then replace the part 1 with "0."
The strstr method:
$string = '43966.31875';
echo '0'.strstr($string, '.');
Also split the number into 2 parts and take the second part and add "0" before
$number = 43966.31875;
$decimalPart = fmod($number, 1);
$decimalCount = explode('.', $number)[1];
$result = round($decimalPart, strlen($decimalCount));
Refer the Official Documentation:
PHP fmod
This question already has answers here:
How can I format a number into a currency value without php?
(3 answers)
Closed 4 years ago.
I am calculating result of my expression using eval() function but its showing all the numbers afer decimal and I want to show only two numbers after decimal
this is my eval function with other data that I have stored.
$expression = generate_expression($num_operands,
$operations, $num_digits);
$expression_string = implode(" ", $expression);
$result = eval("return ($expression_string);");
$expressions[] = compact("expression", "result");
I am storing the expression_
for this I tried using round function also but its not showing the result that I want
here is the round function for the $result
<?php echo round($result,2); ?>
Where I went wrong?
Use: number_format()
<?php echo number_format((float)$result, 2); ?>
Example
This question already has answers here:
Formatting numbers with commas PHP [duplicate]
(4 answers)
Closed 7 months ago.
I need split by gap like when we write count numbers we will give gap for every 3 integer.
echo chunk_split(1000255869,3," ");
I got output like: 100 025 586 9
But I need output like: 1,000,255,869
How to reverse that one?
Try this,
<?php
$a = 1000255869;
echo $a;//output - 1000255869
$a = number_format($a);
echo $a;//output - 1,000,255,869
?>
if you want to do a number format, you should do it with number_format.
echo number_format(1000255869,0,".",",");
This should work number_format(1000255869, 0, '.', ',');
Try this.Reference
$number= 1000255869;
echo number_format($number);
This question already has answers here:
Php Regular Expression repeated characters
(2 answers)
Closed 9 years ago.
I've got a Call Me Back form which sends me a phone number of a person who wants to be called back. Today I received the form with '88888888' instead of a real phone number.
How can I check if the string contains 1 and the same number, continous?
There must not be more than 4 same numbers in a row.
To check if the string contains only one repeated integer, string -> array, check if the unique count is 1.
<?php
$string = "88888888";
$array = array_unique( str_split( $string ) );
$result = $array;
if( count($result) === 1 ) {
echo "Same number repeated in string";
}else{
echo "More than 1 number found in string";
}
?>
-Edit-
optimized: Removed for loop thanks to comment by #Uberfuzzy
$number_string = (string)$number_string;
return strlen($number_string) > 0 && str_repeat($number_string[0], strlen($number_string)) === $number_string;