I have a code like this $new_amount = round(($old_amount/53),2);
amount may be 1,2, ...,10000;
How get exact $old_amount from $new_amount; Now i tried to use
$old_amount = ceil($new_amount);` AND `$old_amount = floor($new_amount);
but not working properly; please help me;
You can't, once you round a value you get rid of the extra precision. This precision is lost. However, the old value is still stored in the $old_amount variable.
If you need the exact amount, don't change the $old_amount while creating $new_amount. So you can use $old_amount later.
There is no way to back. Both Ceil() and floor() functions can't give you what you want.
Related
So, i retrieved with an API a number, which is 1441971834999104426.
I would like to round it to 1441971835
However, when i do : round(1441971834999104426, -9); the output is : 1.441971835E+18, which is not what i want at all.
Can you give me some advices on how I can do the rounding perfectly?
Thanks to you.
Divide the number by 1e9, and round that.
echo round(1441971834999104426/1e9);
I've been playing about with the uniqid but it started giving me the 13 long string.
I'm looking for a prefix of 100 with up to 6 random numbers afterwards
thanks any help appreciated
function generate_order(){
$order_ref="";
$a=uniqid(prefix,100);
$num4=array('0','1','2','3','4','5','6','7','8','9');
$num=rand(0,9);
$num2=rand(0,9);
$num3=rand(0,9);
shuffle($num4);
//now the final
$order_ref = $num4[0].$num4[3].$num.$num4[1].$num2.$num4[2].$num3.$num4[4];
}
I've got some remakrs to the code above:
Why are you doing a shuffle() on an array with numbers ranging from 1 to 9? This is the same as doing rand(1,9)
Taking point one into account, using rand(10000000, 99999999) will give you the same result.
$a=uniqid(prefix,100) is not used in your function
The prefix in uniqid() should be a string.
Your function doesn't return. You should use return $order_ref;
Hope these will help you fix your function.
In response to your comment
$order_ref = '100'.rand(100000, 999999);
Additional suggestions
If you are planning to use this as an order reference as I suspect, I do not recommand just using random numbers. This will give you a big chance of having duplicate numbers.
Instead, I suggest using $order_ref = '100'.date('u').rand(10, 99);. This will give you a random number based on the current time and thus prevent (or at least minimize) the chance of duplicate order references.
If you want 100 before your result, then use the prefix to enter your desired string
Using uniqid(100) should give you the desired result.
Here is how I would do
1. Define your prefix
2. Generate random number
3. Concat both
$prefix="some_prefix";
$rand_no = rand(1,100);
$rand_no_with_prefix = $prefix.$rand_no;
You can Google for exact syntax and functions.
This is just basic logic I would follow.
Hope it helps.
Also step 4 would be to parse if required(if concated random number needs to be integer)
I have read the documentation and have clear idea of round but i didn't find useful information to solve the problem.
The problem is i have float number which is let say 1.09 and i want to display it 2 instead of 1. if we use round function it display 1. Help me solve this problem.
MORE DETAILS...
$TotalPaidRemaining=1090;
$monthly_installments=1000;
$MakingNumberOfMonths=$TotalPaidRemaining/$monthly_installments;
echo round($MakingNumberOfMonths, 0, PHP_ROUND_HALF_UP);// it display 1. i want it display 2..
What i want is if the value after decimal point is greater than 0. For example 0.01. I want to consider it as 1.
Hope i am clear at my question.
Use the ceil() function instead.
$number = ceil(0.1); // $number will be 1
From the documentation :
Returns the next highest integer value by rounding up value if necessary.
You can use the ceil() php function instead of round(). It will round up your values. Docs: http://php.net/manual/en/function.ceil.php
Example:
ceil(1.09); // return 2
You could use ceil($yourNumber), which will round the number to its next higher integer.
Or you could use round($yourNumber + 0.499999999999999).
Or you could use floor($yourNumber + 1), which rounds the number to its previous highest integer.
I'm creating an import based on xml. From the XML i get some values wich are negative amounts like -316.65.
Before I save this values to the database I have to convert them to positive amounts. I've tried the following:
$amount = $inkoopfactuur->td[7] * -1;
This worked but, the decimals where gone.
Is there a way to convert this witouth losing the decimals?
Thanks in advance!
There's a better way to do this:
$amount = abs($inkoopfactuur->td[7]);
However, it could be the case that the integer rounding is the result of something else -- there's not enough information in the question to be certain.
You probably have a different locale.
Check localeconv
I have a number stored in a variable. The number is pulled from the database as 9900..I need to convert this number to 99.00 in order to display to a customer in HTML.
I was wondering how can I achieve this in php.
Thanks.
$priceInCents = 9900;
$priceInDollars = $priceInCents / 100;
And then you can use round() or number_format() as you please.
You can use the number_format() function for that:
echo number_format(9900/100);
You can do this by either using money_format or number_format, if you are going to display it as a price you should look at money_format, otherwise, number_format is the way to go.