Re: [Gimp-user] does number->string have format options?




does number->string have format options? What is the equivalent of "printf" in gimp tinyscheme?

I am scripting adding axes to an image and need to control of format of floating point numbers that will be passed to gimps text generation functions.


Here is a helper function I had written to deal with this:

(define (number->fixedstring V D)
  (let ((D (trunc D)))
    (string-append
      (number->string (if (> D 0) (trunc V) (round V)))
      (if (> D 0) (string-append "." (number->string (round (* (- V (trunc V)) (pow 10 (trunc D)))))) "")
    )
  )
)

In use, the first parameter is the value and the second is the number of decimals to include.  It rounds to the decimal specified and pads where zeros if needed.

> (number->fixedstring 3.14159 0)
"3"
> (number->fixedstring 3.14159 1)
"3.1"
> (number->fixedstring 3.14159 2)
"3.14"
> (number->fixedstring 3.14159 3)
"3.142"
> (number->fixedstring 3.14159 4)
"3.1416"
> (number->fixedstring 3.14159 5)
"3.14159"
> (number->fixedstring 3.14159 6)
"3.141590"
> (number->fixedstring 2.7 0)
"3"


-Rob A>


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]