I understand how to format a number from for example 2.10 to 2.1 nut how would i format a number so that 381 -> 38.1% or 38.1
Either result is fine as I can just add a percent sign after the first result. I was thinking of maybe splitting the number then adding a decimal after the second number but this wouldn't work with 100%?
If 381 is a percentage of 1000, then 381 divided by 1000 = 0.381
Multiply 0.381 by 100 to get 38.1
The SQL CODE would be:
DECLARE #Number1 Decimal(18,1) = 381, #Number2 Decimal(18,1) = 0
SET #Number2 = CAST(ROUND((#Number1 / 1000) *(100),1,1) AS DECIMAL(18,1))
SELECT #Number2
Hope this helps.
This should work for you:
$number = 381;
echo sprintf("%02.2f%%", $number/10);
Output:
38.10%
If you don't want it with 2 decimal just change .2 as you want it
Doing $number = $number / 10; should turn the trick.
Related
I have an array and I am getting its length using sizeof(). In the size of the array I want to get how many 10 there are in that number. I used % in getting my number like:
$arraysize = 13;//sizeof($array)
$numberoften = $arraysize % 10 // getting 3
From here I know that I am wrong. What i want to get is 2. Because I want to get every 10. Like if length is 9 I want to get 1. If length is 31 I want to get 4.
What is the correct function of way to get my desired output?
What you're after is ceil()
Returns the next highest integer value by rounding up value if necessary.
$numberoften = ceil($arraysize / 10);
There's also floor() if you want to round the number down to the nearest integer. This would seem to fit better with your question...
I want to get how many 10 there are in that number
Use ceil() function
$arraysize = 13;//sizeof($array);
$numberoften =ceil( $arraysize/10);
Or
Use round() function
$arraysize = 13;//sizeof($array);
$numberoften =round( $arraysize/10);
I don't know how to title the question correctly (please tell me what this kind of number called).
I'd like to convert from 2 digits number into a 10 divisible number. For example, I expect:
15 to become 10
23 to become 20
38 to become 30
999 to become 900
9999 to become 9000
I tried searching for the solution on google but I don't know how to type the proper word.
Easy, use the PHP floor function: http://php.net/manual/en/function.floor.php
floor($number/10) * 10
I wrote a simple function should work:
<?php
function roundDown($var){
$len = strlen($var)-1;
$divide = 1;
for($i=1;$i<=$len;$i++){
$divide .= 0;
}
return floor($var/$divide)*$divide;
}
echo roundDown(9999);
Easiest way is divide by ten, then floor value and multiply by ten. (Floor method - phpdocs)
floor($number/10)*10
Use round:
echo round(1241757, -6); // 1000000
from http://php.net/manual/en/function.round.php
You are looking for the "one significant figure".
I think this answer provides a good solution:
How to round down to the nearest significant figure in php
Short form:
$x = $y - $y % pow(10, floor(log10($y)));
Searching for a function ro round numbers to the nearest multiple of 5
22 -> 20
23 -> 25
40 -> 40
46 -> 45
48 -> 50
and so on.
Tried this which always returns the higher value:
5 * ceil($n / 5);
Use round() instead of ceil().
5 * round($n / 5);
ceil() rounds a floating point number up to its next integer in sequence. round() will round to the nearest integer using standard rounding rules.
Back to maths, since round works with decimals, multiply by 5 and divide by 10 and then round, it. Multiply by 5 again to get what u want. (Other answer works as well, just a different way of looking at it)
function round_5($in)
{
return round(($in*2)/10)*5;
}
echo round_5(48);
See if this helps
Well, facing this issue while helping make an POS for a Canadian company, came up with this solution, hope it helps someone. (Canada removed the penny in 2012). Also includes for doing tax included pricing, just pass '1' as second argh.
//calculate price and tax
function calctax($amt,$tax_included = NULL){
$taxa = 'tax rate 1 here';
$taxb = 'tax rate 2 here';
$taxc = ($taxa + $taxb) + 1;
if(is_null($tax_included)){
$p = $amt;
}else{
$p = number_format(round($amt / $taxc,2),2);
}
$ta = round($p * $taxa,2);
$tb = round($p * $taxb,2);
$sp = number_format(round($p+($ta + $tb),2),2);
$tp = number_format(round(($sp*2)/10,2)*5,2);
$ret = array($ta,$tb,$tp);
return $ret;
}
How to make number like 3.0000000054978E+38 to 3.00 in PHP?
Many thanks!
You cannot use round to solve this since it is a number in scientific notation. You can, however, use substr:
$i = 3.0000000054978E+38;
$i = substr($i, 0, 2); // $i is now the string 3.00
echo( number_format($i+1,2) ); // Will output 4.00
In case you are looking for the small fraction of your number being outputted in a formatted fashion:
$number = 3.0000000054978E+38;
printf('%.2f', $number / 1E+38); # 3.00
You can just use round, as in round($floating_number, 2).
sprintf() always gives you the specified number of decimal points, if you require.
sprintf('%0.2f', 3.0000000);
Would display 3.00, if you echo it.
Using PhP I want to make my numbers more 'readable'...
I want to modify for example:
10 ... 10 (no change)
100 would be 100 (still no change)
1000 would be 1.000
1.000.000 would be 1.000.000
1000000000 would be 1.000.000.000 etc
number_format()
You're looking for NumberFormatter.
$number = 1000000000000.4568;
$str_num = number_format( $number, 2, '.', '.' );
// $str_num Output
1.000.000.000.000.46