Recent

Author Topic: Rounding decimal numbers  (Read 29564 times)

M[a]nny

  • Full Member
  • ***
  • Posts: 122
  • Dreamer
Rounding decimal numbers
« on: August 03, 2010, 01:27:27 pm »
Hello, guys!

I think the most of you know this function:
Code: Pascal  [Select][+][-]
  1. function Round ( const Number  : Extended ) : Int64;
Example:
Code: Pascal  [Select][+][-]
  1. result := Round(15.5); // result := 16

This function works great but is there any way how to round decimal values, either?
PHP has round function, too:
Code: PHP  [Select][+][-]
  1. echo round(1.95583, 2);  // will return 1.96
... but as you can see, there is second parameter which defines how many decimal places would be taken.

Does exist similar function here, in Object Pascal?
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

Laksen

  • Hero Member
  • *****
  • Posts: 649
    • J-Software
Re: Rounding decimal numbers
« Reply #1 on: August 03, 2010, 01:35:50 pm »
I don't think so. A functional equivalent could be

Code: Pascal  [Select][+][-]
  1. uses math;
  2. ...
  3. function round2(const Number: extended; const Places: longint): extended;
  4. var t: extended;
  5. begin
  6.    t := power(10, places);
  7.    round2 := round(Number*t)/t;
  8. end;
  9.  

If you need it for string formatting, there are attributes that control decimal places in most of the formatting functinos

M[a]nny

  • Full Member
  • ***
  • Posts: 122
  • Dreamer
Re: Rounding decimal numbers
« Reply #2 on: August 03, 2010, 01:52:02 pm »
Thank you :) Beauty in simplicity.
Bad news: Time flies.
Good news: You are the pilot.

Don't try to be perfect, just be unique.

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
Re: Rounding decimal numbers
« Reply #3 on: August 03, 2010, 04:00:02 pm »
There RoundTo in the math unit.

 

TinyPortal © 2005-2018