Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 2253 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

php rounding number

Thread title: php rounding number
Closed Thread    
    Thread tools Search this thread Display Modes  
12-08-2006, 01:10 AM
#1
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,894
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old  php rounding number

I need to round off a number (non-float) to the next 10, not the closest 10. I have been trying for a long time and cant figure out how to do it.

12-08-2006, 01:30 AM
#2
Andrew R is offline Andrew R
Status: Request a custom title
Join date: Dec 2005
Location: Arizona
Expertise:
Software:
 
Posts: 5,200
iTrader: 17 / 95%
 

Andrew R is on a distinguished road

  Old

Here is a simple function. I've tested it and it works.

PHP Code:
<?php
function round_up($num)
{

    
$exp explode("."$num);
    
$new_num $exp[0]+1;
    echo 
$new_num;
}

echo 
round_up(5.06);
?>
This will output: 6

You can add a preg_match() to the function to make sure that only numbers will work. (i.e. Text.text won't be turned through the function)

12-08-2006, 01:32 AM
#3
MaxS is offline MaxS
Status: Senior Member
Join date: Aug 2005
Location: New York
Expertise:
Software:
 
Posts: 825
iTrader: 1 / 100%
 

MaxS is on a distinguished road

  Old

Just use modulus and get the remainder when dividing by ten. If the remainder is 5 or greater, add 10-remainder to it.

Edit: Wait, the next ten? Ok. Modulus 10. Do 10-remainder and add it to the number.

I'm assuming I misunderstood your first post. Otherwise, it's a fairly simple algorithm. I'll post the code if you need it.

Good luck

12-08-2006, 01:45 AM
#4
stropple is offline stropple
Status: I'm new around here
Join date: Oct 2006
Location:
Expertise:
Software:
 
Posts: 13
iTrader: 0 / 0%
 

stropple is on a distinguished road

  Old

original number is $number, rounded up is $round.

$num = substr(floor($number), 0, -1);
$round = ++$num*10;

12-08-2006, 01:47 AM
#5
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,894
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old

ends up that wasent what I needed to do for the problem I had. thanks anyway

12-08-2006, 01:53 AM
#6
stropple is offline stropple
Status: I'm new around here
Join date: Oct 2006
Location:
Expertise:
Software:
 
Posts: 13
iTrader: 0 / 0%
 

stropple is on a distinguished road

  Old

Originally Posted by Andrew R
Here is a simple function. I've tested it and it works.

PHP Code:
<?php
function round_up($num)
{

    
$exp explode("."$num);
    
$new_num $exp[0]+1;
    echo 
$new_num;
}

echo 
round_up(5.06);
?>
This will output: 6
An even simplier function - ceil() :P

12-08-2006, 01:56 AM
#7
MaxS is offline MaxS
Status: Senior Member
Join date: Aug 2005
Location: New York
Expertise:
Software:
 
Posts: 825
iTrader: 1 / 100%
 

MaxS is on a distinguished road

  Old

Originally Posted by stropple
An even simplier function - ceil() :P
Yea, ceil will do it but that's not what he's asking for. My solution would work.


I need to round off a number (non-float) to the next 10, not the closest 10.

Closed Thread    


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed