Jump to content

round numbers


dflow

Recommended Posts

It's very crude, but would something like this work:

 

<?php
//INITIALIZE VARIABLES
$price = 85;
$percent = 15;
$percentIncrease = $price * ($percent/100);

//GET NEW PRICE
$newPrice = $price + $percentIncrease;

//REMOVE DECIMAL
$newPrice = (int)$newPrice;  //you may want to use     $newPrice = round($newPrice);

//REPLACE LAST NUMBER WITH A NINE (and display results before and after)
echo "<div>New price <b>before</b> rounding: $newPrice</div>";
$newPrice = substr($newPrice, 0, -1) . 9;
echo "<div>New price <b>after</b> rounding: $newPrice</div>";
?>

 

 

Of course this won't work if the final price needs to be round down. It also doens't work if the $newPrice is 109.99 for example, but this could be fixed by using the round() or ceil() function.

Link to comment
https://forums.phpfreaks.com/topic/230759-round-numbers/#findComment-1188009
Share on other sites

it's psychological cognitive perception of price,

it actually works

 

Agreed that it works.  My point is that personally, even as a ladd, I always shook my head at the practice.  It wasn't until I studied law that I realized that not all of mankind is intelligent. 

 

Anyways, how's the progress?

Link to comment
https://forums.phpfreaks.com/topic/230759-round-numbers/#findComment-1188349
Share on other sites

this could be an option actually :), but then ill need to isolate the ones digit, how would i do that?

 

 

You could utilize the code I posted earlier:

 

substr($newPrice, 0, -1)

 

 

Of course, you'll need to remove the decimal part first:

 

$newPrice = (int)$newPrice;

 

$newPrice = round($newPrice);

Link to comment
https://forums.phpfreaks.com/topic/230759-round-numbers/#findComment-1188351
Share on other sites

It's very crude, but would something like this work:

 

<?php
//INITIALIZE VARIABLES
$price = 85;
$percent = 15;
$percentIncrease = $price * ($percent/100);

//GET NEW PRICE
$newPrice = $price + $percentIncrease;

//REMOVE DECIMAL
$newPrice = (int)$newPrice;  //you may want to use     $newPrice = round($newPrice);

//REPLACE LAST NUMBER WITH A NINE (and display results before and after)
echo "<div>New price <b>before</b> rounding: $newPrice</div>";
$newPrice = substr($newPrice, 0, -1) . 9;
echo "<div>New price <b>after</b> rounding: $newPrice</div>";
?>

 

 

Of course this won't work if the final price needs to be round down. It also doens't work if the $newPrice is 109.99 for example, but this could be fixed by using the round() or ceil() function.

 

gr8 thanks

Link to comment
https://forums.phpfreaks.com/topic/230759-round-numbers/#findComment-1188415
Share on other sites

This would do the trick in most cases i think:

 

In this example the price is set to 7.51

 

$price = 7.51;
$new_price = explode(".",$price);

if ($new_price[1] > 50) {
$cent=99;
}
else if ($new_price[1] < 50) {
$cent=50;
}

$ending_price = "$new_price[0].$cent";

 

 

In this example the ending price would be 7.99

 

 

Link to comment
https://forums.phpfreaks.com/topic/230759-round-numbers/#findComment-1188429
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.