📄 round.gwyn.html
字号:
<html><!-- Mirrored from c-faq.com/fp/round.gwyn.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:03:00 GMT --><head><title></title></head><body><p>From: Douglas A. Gwyn<br>Subject: Re: precision of a double ?<br>Date: 18 Aug 2000 00:00:00 GMT<br>Message-ID: <clcm-20000818-0004@plethora.net><br>Newsgroups: comp.lang.c.moderated<p>"rhim.m" wrote:<br>> I'm looking for a function such as<br>> <TT>void setprecision(int precision , double in, double *out )</TT> .<br>> this function can set the precision of a double (numbre of decimals ).<br>> for exemple , a double "35.425" and a precision "1" are the inputs ;<br>> the output is "35.4".<p>This is usually called ``rounding''. It's really not very hard:<pre> #include <assert.h> #include <math.h> double round2mod(double x, double module) { if (module == 0) return x; assert(module > 0); if (x < 0) return -round2mod(-x, module); return x + module/2 - fmod(x + module/2, module); } void setprecision(int precision, double in, double *out) { *out = round2mod(in, pow(10, -precision)); }</pre>-- <br>comp.lang.c.moderated - moderation address: clcm@plethora.net</body><!-- Mirrored from c-faq.com/fp/round.gwyn.html by HTTrack Website Copier/3.x [XR&CO'2008], Sat, 14 Mar 2009 08:03:00 GMT --></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -