How to round up from the hundredth point in php [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 1 year ago.
Improve this question
I'm trying to find a way to turn convert 36.901775282237 to 36.91 in php which function do I use in order to convert to this value. I've tried the round function but it's not giving me the right value no matter what flag I use.

The normal way is to use number_format
<?php
$newvalue=number_format(36.901775282237,2);
echo $newvalue;
?>
But if you wish to do what you want , then use a few lines of codes. (I think no need to further explain, just simple math)
<?php
$value=36.901775282237;
//$value=36.900775282237; for this case the result will be 36.90
///// do the trick
$value=intval($value * 1000)/1000;
$parsex=number_format($value * 100,2, ".", "");
$parsex=ceil($parsex)/100;
//// end
echo $parsex;
?>

Mathematically speaking, 36.901775282237 doesn't round up to 36.91 but rather rounds down to 36.90. You'll need to combine ceil with some custom math to get the rounding you're asking for. If you're looking for mathematically correct rounding, number_format will accomplish that internally:
function ceilWithPercision($number, $decimalPoint) {
$number = $number * pow(10, $decimalPoint);
$number = ceil($number);
$number = $number / pow(10, $decimalPoint);
return (string) $number;
}
$number = 36.901775282237;
var_dump(ceilWithPercision($number, 2)); // "36.91"
var_dump(number_format($number, 2)); // "36.90"
Example here: https://3v4l.org/NMOpH

You should take a look at some of the Php Documentation, and maybe some tutorials online before asking these kinds of questions here.
What I think you are looking for is number_format(36.901775282237, 2)

Related

How can I do in PHP to be similar to Excel Rounddown Formula [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed last month.
Improve this question
In excel rounddown,
rounddown(6432737, -7) = 6430000
rounddown(3456484, -7) = 3450000
How can I do it in PHP?
Please, I appreciate your ideas and answers.
I tried with floor in PHP but it doesn't work.
You can use the floor function in PHP to round down a number to the nearest multiple of a power of 10. For example:
$rounded = floor(6432737 / 10000000) * 10000000; // $rounded will be
6430000
$rounded = floor(3456484 / 10000000) * 10000000; // $rounded will be
3450000
The floor function rounds down a number to the nearest integer, so you can use it to round down a number to the nearest multiple of any number. In the example above, we are dividing the number by 10,000,000 and then multiplying it by 10,000,000 to get the original number rounded down to the nearest multiple of 10,000,000.
You can also use the round function with a negative value for the second argument to achieve the same result:
$rounded = round(6432737, -7);
// $rounded will be 6430000
$rounded = round(3456484, -7);
// $rounded will be 3450000

php divide with 'easy' numbers [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I'm working on a program in PHP that lets you train for making calculations without a calculator. The problem I run into is that when generating random numbers for the sums you can get awkward numbers like 4 divided by 7. Is there an easy way to generate easy sums like 9 divided by 3?
Thanks!
The easiest method is to generate two random numbers to multiply together. Get the answer to the multiplication problem. Then use the answer along with one of your original random numbers for your division problem. The answer will always be the second random number you generated.
Pseudo code:
$random1 = random(1, 100);
$random2 = random(1, 100);
$answer = $random1 * $random2;
print("What is $answer / $random1?"); // answer is always $random2
You don't explain what range of random numbers you are using, but be careful not to pick 0 for $random1.
suppose there are two numbers you get at random $a and $b. If u want to divide $a by $b check if $a>$b and then check if remainder is zero. $a%$b==0; if yes you can echo the values.
I've not tested this code, but it should check if the division is easy or not, using the modulo character %, and call the function again if it is not easy. If the division is easy, it returns the numbers as an array.
function easyDivide() {
// Code for generating random numbers
if($num1 % $num2 != 0) {
return easyDivide();
} else {
return array($num1, $num2);
}
}

Decimal number regular expression, no any . {dot or point } Using PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
For example if I had:
1.2
1.65
5
9.5
125
Valid numbers would be 5 and 125.
Invalid numbers : 1.2, 1.65, 9.5
I am stuck in checking whether a number has a decimal or not.
I tried is_numeric but it accepted numbers with a decimal.
A possibility is to try using strpos:
if (strpos($a,'.') === false) { // or comma can be included as well
echo 'Valid';
}
or try it using regex:
if (preg_match('/^\d+$/',$a))
echo 'Valid';
Examples taken from here.
If you are sure the number variable is not a string you can use is_int() to check whether it is valid or is_float() to check whether it is invalid.
But if you handle forms for example the variable is often a string which makes it harder and this is an easy solution which works on strings, integers and floats:
if (is_numeric($number)) {
//if we already know $number is numeric...
if ((int) $number == $number) {
//is an integer!!
}
}
It's also faster than regex and string methods.
Use is_float() function to find invalid numbers

Reduce three lines of arithmetic to one line? Divide and round [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I have an integer value which will always be a whole number, called $post_count
I want to divide $post_count by 2. So if it's an even number, it will always produce a whole result. E.g if $post_count = 8 then I want the result of my arithmetic to be 4.
However if it's an odd number, I wish to presented with the rounded-up number. So if $post_count = 7, I would still want the answer to be 4, because the maths is =
7 / 2 = 3.5
3.5 rounded up = 4
I've written the following code but I am wondering if it's possible to reduce this quite lengthy code into something simpler?
$post_count = $the_query->found_posts;
$post_count = $post_count / 2;
$post_count = round($post_count);
You can use ceil -
$post_count = ceil( $the_query->found_posts / 2 );
If $the_query->found_posts = 7 then it will print 4. ceil will always return the next bigger integer of the current number.
<?php
$posts = 7;
echo round($posts/2);
// 4
You can do like this:
$post_count = round($the_query->found_posts/2);

Split Float & Replace Integer PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I have float values in an array... Let's say one of my values is:
5.1234
How do I SWAP the integer in the float. So in the example above, I'd like to swap the 5 with 8. Therefore the new number would be:
8.1234
This needs to be a SWAP, not a mathematical addition as in 5.1234 + 3.
I basically need to split the number in two, the integer (5) and the float value following it (.1234), swap the 5 for the 8 and the recombine them to get 8.1234.
What is the fastest and most elegant way to do this in PHP since I'll be using this on a LOT of data?
To clarify WHY math cannot be used: This is because this is an obj file that's looking for an usemtl library title (Mudbox compliant) from which it extracts the UV space. Then it changes the vert U (or V) accordingly. Problem is these faces may come up more than once. This would make the operation cumulative, which it is NOT. All it needs to do is substituted the integer.
<?php
$number = 5.1234;
$array = explode(".", $number);
// $array[0] contains 5
$newNumber = 8;
$array[0] = $newNumber;
$finalString = $array[0] . '.' . $array[1];
$finalFloat = floatval($finalString); // String to float
echo $finalFloat;
?>
Here is how I would do this. This solution is relevant if you are sure the number will always be formated like followed :
[number].[decimals]
Else you will not be able to always replace the number before the dot.

Categories