[SOLVED] closest number?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shark86
Forum Newbie
Posts: 2
Joined: Mon Jun 14, 2004 11:38 am

[SOLVED] closest number?

Post by shark86 »

Hey,
I have a problem.

I have a DB table with a collume in witch i saved numbers.
1.123
1.234
4.67854
...
I need to get the number that is closest or equal to 2.3. In that example 1.234.
How do I do that? If I use like or = in a MySQL DB query I get just a replay if 2.3 is in the DB, but not the closest.
I looked a usort, but i wasen't able to do it.

greetings an thanks
shark86
Last edited by shark86 on Tue Jun 15, 2004 11:41 am, edited 1 time in total.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Code: Select all

select numberr from numbers order by abs($num_to_search_for-numberr) limit 1
shark86
Forum Newbie
Posts: 2
Joined: Mon Jun 14, 2004 11:38 am

Post by shark86 »

Thank you so much, it works fine

shark
http://www.sebastian-haak.net
Post Reply