Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6592009
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T17:29:57+00:00 2026-05-25T17:29:57+00:00

I found this code on php.net which works for me for the round_up part..

  • 0

I found this code on php.net which works for me for the round_up part..

function round_up($value, $precision = 0) { 
 if (!empty($value)) {
 $sign = (0 <= $value) ? +1 : -1; 
 $amt = explode('.', $value); 
 $precision = (int) $precision; 
 if (strlen($amt[1]) > $precision) { 
     $next = (int) substr($amt[1], $precision); 
     $amt[1] = (float) (('.'.substr($amt[1], 0, $precision)) * $sign); 
     if (0 != $next) { 
         if (+1 == $sign) { 
             $amt[1] = $amt[1] + (float) (('.'.str_repeat('0', $precision - 1).'1') * $sign); 
         } 
     } 
 } 
 else { 
     $amt[1] = (float) (('.'.$amt[1]) * $sign); 
 } 
 return $amt[0] + $amt[1]; 
 }
 else {echo 'error';} 
 }

Added a couple minus’s.. to get this one…

function round_down($value, $precision = 0) { 
 if (!empty($value))  {
 $sign = (0 <= $value) ? +1 : -1; 
 $amt = explode('.', $value); 
 $precision = (int) $precision; 
 if (strlen($amt[1]) > $precision) { 
     $next = (int) substr($amt[1], $precision); 
     $amt[1] = (float) (('.'.substr($amt[1], 0, $precision)) * $sign); 
     if (0 != $next) { 
         if (-1 == $sign) { 
             $amt[1] = $amt[1] - (float) (('.'.str_repeat('0', $precision - 1).'1') * $sign); 
         } 
     } 
 } 
 else { 
     $amt[1] = (float) (('.'.$amt[1]) * $sign); 
 } 
 return $amt[0] + $amt[1]; 
 }
 else {echo 'error';}
 } 

Is there any better way to do it? (I only require it for positive decimals) Currently for the most part it works without much glitches..

$azd = 0.0130; 

$azd1 = number_format(round_up($azd,2),4);
$azd2 = number_format(round_down($azd,2),4);
$azd3 = number_format(round_up($azd,1),4);
$azd4 = number_format(round_down($azd,1),4);

echo 'Round_up = '.$azd1.'<br>'; // 0.0200
echo 'Round_down = '.$azd2.'<br>'; // 0.0100
echo 'Round_up = '.$azd3.'<br>';    // 0.1000
echo 'Round_down = '.$azd4.'<br>';  // 0.0000
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T17:29:57+00:00Added an answer on May 25, 2026 at 5:29 pm
    function round_up($value, $precision=0) {
        $power = pow(10,$precision);
        return ceil($value*$power)/$power;
    }
    function round_down($value, $precision=0) {
        $power = pow(10,$precision);
        return floor($value*$power)/$power;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my code: function get_remote_file_to_cache(){ $sites_array = array(http://www.php.net, http://www.engadget.com, http://www.google.se, http://arstechnica.com, http://wired.com); $the_site=
I have the following php code which I found here : function download_xml() {
I'm using the source code found on this site here: http://webtint.net/tutorials/5-star-rating-system-in-php-mysql-and-jquery/comment-page-1/#comment-2562 A fantastic resource,
I found this PHP code in an app I have to modify... $links =
I found the following code to create a tinyurl.com url: http://tinyurl.com/api-create.php?url=http://myurl.com This will automatically
I'm using a function I found on PHP.net that recursively examines a directory and
I have seen this question before and found this example in http://www.zeitoun.net/articles/comet_and_php/start which is
Possible Duplicate: Find url from string with php I found this code: but 2
i found this code: protected override void OnSourceInitialized(EventArgs e) { base.OnSourceInitialized(e); HwndSource hwndSource =
I found this code using Google. private int RandomNumber(int min, int max) { Random

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.