Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control decimal point in Common Lisp format?

Tags:

common-lisp

How to pass a value to a function that control how many number in decimal point to print.

I can do with $ or f, e.g

(format t "~,3f" 3.141592)
(format t "~2$" 3.141592)

But how to do something like this

(defun control-format (x)
  (format nil "~,xf" 3.141592))

So we can pass any number as argument to control-format to control how many decimal number we want.

Thanks.

like image 846
azzamsa Avatar asked Feb 01 '26 02:02

azzamsa


1 Answers

Use v instead of a literal number:

* (dotimes (i 8)
    (format t "~,vf~%" i pi))
3.
3.1
3.14
3.142
3.1416
3.14159
3.141593
3.1415927
NIL
*
like image 55
jlahd Avatar answered Feb 04 '26 01:02

jlahd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!