linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Per Jessen <per@computer.org>
To: linux-c-programming@vger.kernel.org
Subject: Re: Remove x number of decimals from a float value
Date: Tue, 11 Jul 2006 21:08:06 +0200	[thread overview]
Message-ID: <44B3F716.4040703@computer.org> (raw)
In-Reply-To: <Pine.LNX.4.64.0607111209430.16861@ns.isi.ulatina.ac.cr>

Fabio Miranda Hamburger wrote:
> Hello,
> 
> I am trying to implement a 100% math function ( avoid handle the float 
> as a char array), that remove x number of decimals from the right to the 
> float point.
> 
> Example:
> Given x= 12345.6789 I call the function with (x,2) and the returned 
> number is: x= 12345.67, for (x,4) returned value is x=12345
> 
> I was trying to implement this function using datatype casting, abs() 
> and other functions from math.h library.

How about :


double f( double x, int n )
{
         return ( n>0 ? f( x*10,n-1 )/10 : floor(x) );
}

Oops, 'n' is number of decimals you want to keep, not throw away.


/Per Jessen, Zurich

  reply	other threads:[~2006-07-11 19:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2006-07-11 18:10 Remove x number of decimals from a float value Fabio Miranda Hamburger
2006-07-11 19:08 ` Per Jessen [this message]
2006-07-11 20:58 ` Glynn Clements

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=44B3F716.4040703@computer.org \
    --to=per@computer.org \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).