• This forum is the machine-generated translation of www.cad3d.it/forum1 - the Italian design community. Several terms are not translated correctly.

rounding to the second decimal digit

  • Thread starter Thread starter Angelo2449
  • Start date Start date

Angelo2449

Guest
Hello everyone,
are taken with rounding numbers to the second decimal number to compose a table.
I can't make the abdends coincide with their sum because of the roundings, cji has a nice routine?
Thank you.
 
if the problem is to check if two numbers are (almost) equal: ie if they are only for the first decimals, you can compare them with (equal espr1 espr2 approximation)
espr1 = 1.123456 espr2=1.123457
(equal espr2) returns nil
(equal espr1 espr2 0.000001) returns t.
If your intent is to make a rounding you need to sum up 0.005 and compare it with the n not added (taking only 2 digits after the comma). if the two numbers are equal means that the sum of 0.005 did not have a carryover and therefore means that the number had a number less than 5 to the 2 digit decim. (e.g. 0.023 + 0.005 = 0.028 (the 2 did not become 3).
I hope I explained.
 
thanks x11start, following your advice, I wrote this code that converts a real number into a formatted string with two decimals rounded to 5.
also adds missing zeros, it works great!
Maybe it's not the best of style but, as they say, winning team doesn't change.
However it would be interesting to make the code even stylistic improvements.
Thank you.
Code:
 (defun formatted (/numberx whole fraz wholes frazs first second third third secondn numbers)

(setq numerox areax)

(setq whole (fix numberx))
(setq fraz)

(setqinteras)
(Sectq frazs)
(setq frazs (substr frazs 3 3))

(Sectq first (substr frazs 1 1))
(Sectq second (substr frazs 2 1))
(Section 3 (1))

(Sectq III)

(if)
(setq secondn (1+ (atoi second)))
(setq secondn (atoi second)
);

(setq seconds)
(setq seconds (substr seconds 1 1))

(setq numeros (strcatinteros "." first seconds)

(setq areay numeros)

);
 
to round (at the first digit after the comma and not at the second.... but change little) I use:

;
;------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
; rounding
;------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
;
; input :
; number to be rounded (eg. 4.35)
;
; output:
; rounded narro number (e.g. 4.5)
;
;
; the rounding takes place according to the following criteria:
;
; from 0.00 to 0.24 -> 0.0
; from 0.25 to 0.74 -> 0.5
; from 0.75 to 1.0 -> 1.0
;
;
(defun round (number / whole roasted comma)
(setq whole (fix number) ; whole number
(setq comma (- whole number)); part after comma
(cond)
((< comma 0.25)(setq arrot 0.0)); if < of 0.25 round to 0.0 (per defect)
((and (>= comma 0.25)(< comma 0.75))(setq arrot 0.5)); rolled to 0.5 (half)
(t (setq arrot 1.0) ; otherwise round to 1.0 (for excess)
)
(setq narro (+ whole arrot)); recompon the number (full + rounded part)
)
;
 
I use these 2 functions to manage decimals

; function to round a number with delta base
(defun round (n delta)
(* delta (fix (+) 0.49))
)

; function of cutting decimals
(defun fixnum(num dec / many)
(setq many (expt 10.0 dec))
(/ (fix (* many num)))
)

(round 1.45689 0.01) returns 1.46
(round 1.45423 0.01) returns 1.45
(fixnum 1.45689) returns 1.4
(fixnum 1.45689) returns 1.45
(fixnum 1.45689 3) returns 1.456
 

Forum statistics

Threads
44,997
Messages
339,767
Members
4
Latest member
ibt

Members online

No members online now.
Back
Top