[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: guile-question: 3/2 -> "1 1/2"
From: |
Thomas Morley |
Subject: |
Re: guile-question: 3/2 -> "1 1/2" |
Date: |
Mon, 10 Aug 2015 23:26:23 +0200 |
2015-08-10 19:02 GMT+02:00 Thomas Morley <address@hidden>:
> Hi all,
>
> I want to put out 3/2 not as 1.5 but "1½" or "1 1/2"
> Is there a predefined procedure in guile or do I need to define my own?
>
> Thanks,
> Harm
It's a matter of "exactness".
And when #e comes into the game the guile-manual is not verbose about it.
To say the least.
In the end I come up with the below. I decided to return a ist, not a string.
But it feels pretty clumsy, noone with something better?
(define (integer-and-fraction nmbr)
"Return a list with an integer and a fraction build from an exact number
Example: 47/7 -> (6 5/7) "
(let* ((i (inexact->exact (truncate nmbr)))
(rest (string->number (format #f "~a~a" "#e" (- nmbr i)))))
;; return a string
;(format #f "~a~a"
; i
; (if (= rest 0)
; ""
; (string-append " " (number->string rest))))
;; return a list
(list i rest)
))
(display (integer-and-fraction (/ 1111 7)))
Cheers,
Harm