WWW.ИСХОДНИКИ.РУ cpp.sources.ru
java.sources.ru web.sources.ru soft.sources.ru
jdbc.sources.ru asp.sources.ru api.sources.ru

  Форум на исходниках
  C / C++ / Visual C++
  Как округлять числа?

СПРОСИТЬ  ОТВЕТИТЬ
профайл | регистрация | faq

Автор Тема:   Как округлять числа?
Vovan опубликован 11-12-2001 12:01 MSK   Click Here to See the Profile for Vovan   Click Here to Email Vovan  
Как округлять числа? Например с точностью до 1000 и 0.001?
tonik опубликован 11-12-2001 22:49 MSK     Click Here to See the Profile for tonik  Click Here to Email tonik     
до 1000: (int)(num/1000)*1000
до 0.001: (float)((int)(num*1000)/1000)
мне кажется, что так
Finder опубликован 11-12-2001 22:55 MSK     Click Here to See the Profile for Finder  Click Here to Email Finder     
floor-в меньшую сторону
ceil-в большую
а для количества знаков и спользуй
* 100, 1000 и т.д.

#include <math.h>
#include <stdio.h>

void main( void )
{
double y;

y = floor( 2.8 );
printf( "The floor of 2.8 is %f\n", y );
y = floor( -2.8 );
printf( "The floor of -2.8 is %f\n", y );

y = ceil( 2.8 );
printf( "The ceil of 2.8 is %f\n", y );
y = ceil( -2.8 );
printf( "The ceil of -2.8 is %f\n", y );
}


Output

The floor of 2.8 is 2.000000
The floor of -2.8 is -3.000000
The ceil of 2.8 is 3.000000
The ceil of -2.8 is -2.000000


tonik опубликован 11-12-2001 22:57 MSK     Click Here to See the Profile for tonik  Click Here to Email tonik     
пардон, скобочку не туды влепил:
(float)((int)num/1000)*1000

СПРОСИТЬ  ОТВЕТИТЬ
Перейти:


E-mail | WWW.ИСХОДНИКИ.RU

Powered by: Ultimate Bulletin Board, Freeware Version 5.10a
Purchase our Licensed Version- which adds many more features!
© Infopop Corporation (formerly Madrona Park, Inc.), 1998 - 2000.