I have decimal numbers like this 0.00651536 and I am unable to validating this number to see if its greater than other decimal number (e.g: 0.00651537) or not!
here is an example:
//$request->input('price') = 0.00651537
//$product->price = 0.00651536
if($request->input('price') > $product->price) {
// my input is greater than product price
} else {
// my input is not greater than product price
}
Issue: (Update)
As my $product->price is decimal starting with 0. it just keep looking to that 0 and does not validate rest of it while my input number difference with product price is in decimals.
How can I validate my numbers?
PS: These are just sample numbers therefore decimals could be shorter or longer (don't go with this example length)
Have you tried converting it to float?
You can try something like:
if(floatval($request->input('price')) > floatval( $product->price)) {
// my input is greater than product price
} else {
// my input is not greater than product price
}
Related
In Woocommerce settings, I have set 6 decimals in order to get more accurate tax calculation. However, I need all prices and amounts to be displayed with only 2 decimals in frontend, emails etc. I found two functions
add_filter('wc_price_args', 'custom_decimals_price_args', 10, 1);
function custom_decimals_price_args($args) {
$args['decimals'] = 2;
return $args;
}
add_filter( 'wc_get_price_decimals', 'change_prices_decimals', 20, 1 );
function change_prices_decimals( $decimals ){
$decimals = 2;
return $decimals;
}
What is the difference between these and which one should I use?
Note that WC_ROUNDING_PRECISION constant is set to 6 in WC_Woocommerce define_constants() method.
That means that WooCommerce Tax calculation precision is already set on 6 decimals.
Tax calculation precision are based on wc_get_rounding_precision() core function used in WC_Tax Class:
function wc_get_rounding_precision() {
$precision = wc_get_price_decimals() + 2;
if ( absint( WC_ROUNDING_PRECISION ) > $precision ) {
$precision = absint( WC_ROUNDING_PRECISION );
}
return $precision;
}
As you can see WC_ROUNDING_PRECISION constant is prioritized if the displayed price decimal value + 2 is smaller than WC_ROUNDING_PRECISION constant. But as you want to keep displayed prices with 2 decimals, this requires something else.
So you should not increase displayed price decimals and not use wc_price_args or/and wc_get_price_decimals hooks, to increase precision in tax calculation.
If precision of 6 decimals is not enough and you want to get more precision:
How to get more precision on Tax calculations?
The best way to get more precision on tax calculations and keep displayed prices with 2 decimals is to edit WordPress wp_config.php file and add the following lines (where you can increase WC_ROUNDING_PRECISION constant value as you like, here the value is set to 8 for example):
// Change WooCommerce rounding precision
define('WC_ROUNDING_PRECISION', 8);
This will change WC_ROUNDING_PRECISION constant without affecting displayed price decimals.
wc_price_args is used to add extra arguments to the price - it takes in raw price and it can add other stuff to it like for example a currency symbol. When get_price is called it will include this currency symbol in the front end.
https://wp-kama.com/plugin/woocommerce/hook/wc_price_args
wc_get_price_decimals it sets a number of decimals after the 'point' - eg. if you set it to 5, the price will look like 0.00000
https://wp-kama.com/plugin/woocommerce/function/wc_get_price_decimals
I didn't test this but it should do the job.
add_filter('wc_price_args', 'custom_decimals_price_args', 10, 1);
function custom_decimals_price_args($price, $args) {
return number_format((float)$price, 2, '.', '');
}
Alternatively, you can edit the template where your products are and format the get_price with the number_format above.
Let me know if this worked for you.
MySQL data imoprt mongo database.
price float(15,2) in mysql, mongo is not float(15,2).
I want to Determine a var $price have two decimal places.
eg. 100.00 is right, 100 or 100.0 is wrong.
eg.1
$price = 100.00;
$price have two decimal, it's right.
eg.2
$price = 100.0;
$price have not two decimal, it's wrong.
I like to use Regular Expressions to do these things
function validateTwoDecimals($number)
{
if(preg_match('/^[0-9]+\.[0-9]{2}$/', $number))
return true;
else
return false;
}
(Thanks to Fred-ii- for the corrections)
Everybody is dancing around the fact that floating point numbers don't have a number of decimal places in their internal representation. i.e. in float 100 == 100.0 == 100.00 == 100.000 and are all represented by the same number, effectively 100 and is stored that way.
The number of decimal places in this example only has a context when the number is represented as a string. In which case any string function that counts the number of digits trailing the decimal point could be used to check.
number_format($price, $numberOfDecimalDigits) === $price;
or
strrpos($price, '.') === strlen($price) - 1 - $numberOfDecimalDigits;
Trivia: $price should not be called a "float variable". This is a string that happens to represent a float value. 100.00 as a float has zero decimal digits, and 100.00 === 100 as float :
$price = 100.00;
echo $price; // output: 100
$price2 = (float)100;
echo $price === $price2; // ouput: 1
In order for this to work, the number will need to be wrapped in quotes.
With the many scripts I've tested, using $price = 100.00; without quotes did not work, while $price = 100.10; did, so this is as best as it gets.
<?php
$number = '100.00';
echo $number.'<br>';
$count = explode('.',$number);
echo 'The number of digits after the decimal point is: ' . strlen($count[1]);
if(strlen($count[1]) == 2){
echo "<br>";
echo "There is 2 decimal points.";
}
else{
echo "<br>";
echo "There is not 2 decimal points.";
}
After you format the value, you can check with simply splitting the value as string into 2 parts, for example with explode ...
$ex=explode('.',$in,2); if (strlen($ex[1])==2)
{
// true
}
else
{
// false
}
But again, as i've commented already, if you really have floating input, this is just not a reliable way, as floating numbers are without set decimal places, even if they appears so because of the rounding at the float=>string conversion
What you can do, if you really have floating numbers and wish to have xxx.yy format numbers:
1) convert float to string using round($x,2), so it will round to 2 decimal places.
2) explode the number as i've described, and do the following:
while (strlen($ex[1]<2)) {$ex[1].='0';}
$number=implode('.',$ex);
I would use the following function for that:
function isFloatWith2Decimals($number) {
return (bool) preg_match('/^(?:[1-9]{1}\d*|0)\.\d{2}$/', $number);
}
This will also check if you have only one leading 0 so number like 010.23 won't be considered as valid whereas number like 0.23 will.
And if you don't care about leading 0 you could use simpler method:
function isFloatWith2Decimals($number) {
return (bool) preg_match('/^\d+\.\d{2}$/', $number);
}
Of course numbers need to be passed as string - if you pass 100.00 won't be considered as true, whereas '100.00' will
I have a price database that stores numbers as floating point. These are presented on a website. Prices can be in the format.
x.x (e.g. 1.4)
x.xx (e.g. 1.99)
x.xxx (e.g. 1.299) <-- new price format
I used to use the string format or %.2f to standardize the prices to two decimal places but now I need to show 3 as well but only if the price is 3 decimal place long.
e.g. 1.4 would display 1.40
1.45 would display 1.45
1.445 would display 1.445
The above formats would be the desired output for the given input.
using %.3f shows all with 3 digits.
e.g. 1.4 would display 1.400
1.45 would display 1.450
1.445 would display 1.445
But that is not what i want does anyone know the best way to do the following.
i.e. any number should display 2 decimal places if it has 0 1 or 2 decimal places
if it has 3 or more decimal places it should display 3 decimal places
I would just format it to three places, then trim a final 0.
$formatted = number_format($value, 3, ".", "");
if (substr($formatted, -1) === "0") $formatted = substr($formatted, 0, -1);
Use this dude
number_format($data->price, 0, ',', '.');
http://php.net/manual/en/function.number-format.php
Here is what I did due to the need to cope with some special cases I had in the app.
count the number of dec places ($prices is a float from the database).
format based on the count in the places using a switch statement.
For all cases with less than 3 decimal places format with 2 (except zero)
For all other case format with 3.
$decimals = strlen(substr(strrchr($price,"."),1));
switch ($decimals) {
case 0: {
if ($price != 0) {
$price = number_format($price),2);
}
break;
}
case 1: {
$price = number_format($price),2);
break;
}
case 2: {
$price = number_format($price),2);
break;
}
default: {
$price = number_format($price),3); // three dec places all other prices
break;
}
}
Thanks for the help...
I have two variables in a PHP program for billing statements, $charges and $payments.
$charges is the total amount due before any payments. $payments is the total amount received.
I calculate the balance due like so:
$balance_due = $charges-$payments;
Simple, except I am getting the following result:
$balance_due has -9.0949470177293E-13 for a value (expecting 0).
Both $charges and $payments have a value of 5511.53.
When I var_dump($charges) and var_dump($payments) they both show: float(5511.53)
This code (and === ):
if($charges == $payments){
error_log('they are the same');
}else{
error_log('they are not the same');
}
both result in false.
If I hard code: $charges = $payments = 5511.53; and run it then $balance_due = 0 as expected.
I am confused. What am I missing?
EDIT NOTES
I was able to use a user contributed function by Nitrogen found on the BC Math Functions page that was suggested I look at in order to come up with the following solution:
if(Comp($charges, $payments)===0){
$balance_due = 0;
}else{
$balance_due = ( $charges - $payments );
}
function Comp($Num1,$Num2,$Scale=null) {
// check if they're valid positive numbers, extract the whole numbers and decimals
if(!preg_match("/^\+?(\d+)(\.\d+)?$/",$Num1,$Tmp1)||
!preg_match("/^\+?(\d+)(\.\d+)?$/",$Num2,$Tmp2)) return('0');
// remove leading zeroes from whole numbers
$Num1=ltrim($Tmp1[1],'0');
$Num2=ltrim($Tmp2[1],'0');
// first, we can just check the lengths of the numbers, this can help save processing time
// if $Num1 is longer than $Num2, return 1.. vice versa with the next step.
if(strlen($Num1)>strlen($Num2)) return(1);
else {
if(strlen($Num1)<strlen($Num2)) return(-1);
// if the two numbers are of equal length, we check digit-by-digit
else {
// remove ending zeroes from decimals and remove point
$Dec1=isset($Tmp1[2])?rtrim(substr($Tmp1[2],1),'0'):'';
$Dec2=isset($Tmp2[2])?rtrim(substr($Tmp2[2],1),'0'):'';
// if the user defined $Scale, then make sure we use that only
if($Scale!=null) {
$Dec1=substr($Dec1,0,$Scale);
$Dec2=substr($Dec2,0,$Scale);
}
// calculate the longest length of decimals
$DLen=max(strlen($Dec1),strlen($Dec2));
// append the padded decimals onto the end of the whole numbers
$Num1.=str_pad($Dec1,$DLen,'0');
$Num2.=str_pad($Dec2,$DLen,'0');
// check digit-by-digit, if they have a difference, return 1 or -1 (greater/lower than)
for($i=0;$i<strlen($Num1);$i++) {
if((int)$Num1{$i}>(int)$Num2{$i}) return(1);
else
if((int)$Num1{$i}<(int)$Num2{$i}) return(-1);
}
// if the two numbers have no difference (they're the same).. return 0
return(0);
}
}
}
That solution worked for me. The answer provided by imtheman below also works and seems more efficient so I am going to use that one instead. Is there any reason not to use one or the other of these?
The way I solved this problem when I ran into it was using php's number_format(). From php documentation:
string number_format(float $number [, int $decimals = 0 ])
So what I would do is this:
$balance_due = number_format($charges-$payments, 2);
And that should solve your problem.
Note: number_format() will return a string, so to compare it you must use == (not ===) or cast it back into a (float) before comparison.
What I do
I am making graph of fictitious stock options.
The price is updated each second, with this function
function stockVariation($price,$max_up,$max_down)
{
// Price > 1
if($price > 1)
{
// Calculate
$ratio=(mt_rand(0,$max_up/2)-mt_rand(0,$max_down/2))/1000;
$price+=$ratio;
}
// Price <=1 (we don't want 0 or negative price...)
else
$price+=mt_rand(1,$max_up)/1000;
return round($price,3);
}
I use a max_up and max_down values (from 10 to 100) to make the price change progressively and simulate some volatility.
For example, with max_up : 40 and max_down : 45, the price will progressively go down.
My question
But the problem, is that prices generated are too much volatile, even if max_up = max_down.
The result is "non-natural". (for example +10 points in one day for a base price of 15,000).
Result of price evolution per hour in 24 hour
Perhaps making round($price,4) and divisions by 10 000 instead of 1 000, will be better ?
If anyone have an idea or an advice to generate "natural" prices evolution, thanks in advance.
There are 86400 seconds in a day, so you'll need to divide by a much larger number. And rather than adding and subtracting, you may want to multiply the current price by a factor that's slightly larger or smaller than 1. That would simulate a percentage increase or decrease, rather than an absolute gain or loss.
function stockVariation($price, $max_up, $max_down)
{
// Convert up/down to fractions of the current price.
// These will be very small positive numbers.
$random_up = mt_rand(0, $max_up) / $price;
$random_down = mt_rand(0, $max_down) / $price;
// Increase the price based on $max_up and decrease based on $max_down.
// This calculates the daily change that would result, which is slightly
// larger or smaller than 1.
$daily_change = (1 + $random_up) / (1 + $random_down);
// Since we're calling this function every second, we need to convert
// from change-per-day to change-per-second. This will make only a
// tiny change to $price.
$price = $price * $daily_change / 86400;
return round($price, 3);
}
Building upon the idea, you could use an actual volatility number. If you want e.g. a volatility of 35%/year, you can find the volatility per second. In pseudocode:
vol_yr = 0.35
vol_month = vol_yr * sqrt(1.0/12)
vol_second = vol_yr * sqrt(1.0/(252 * 86400)) # or 365 * 86400
Then, every second, you "flip a coin" and either multiply or divide current stock price by (1 + vol_second). This is the principle of how binomial trees are created to evaluate exotic stock options.