unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* bike shifting - output decimal in percent
@ 2019-05-19  1:34 Emanuel Berg
  2019-05-19  3:50 ` Drew Adams
  0 siblings, 1 reply; 12+ messages in thread
From: Emanuel Berg @ 2019-05-19  1:34 UTC (permalink / raw)
  To: help-gnu-emacs

How do I output a decimal float digit
0 <= x <= 1 into percent? ("A percentage?"
Correct English?)

I asked at #emacs@irc.freenode.net and got the
below answer, which, as you see, works fine.

But I wonder if/why-not there is/isn't
a built-in way to do it?


Context:

;; thanks to friend at #emacs
(defun percent-string (decimal)
  (concat
   (number-to-string
    (round (* 100 decimal)) ) "%") )
;; test: (percent-string 0.42857142857142855) ; "43%"

(defun bike-compute-step (from to)
  (percent-string
   (/ (- from to)
      from
      1.0) ))
;; test - should be 43% for
;; one 42t and one 24t chainring
;; according to an article in the spring 2019 issue
;; of "Bicycle Quarterly" (ISSN 1941-8809) -
;; (bike-compute-step 42 24) ; "43%"


Whole file:

  http://user.it.uu.se/~embe8573/emacs-init/bike.el

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: bike shifting - output decimal in percent
  2019-05-19  1:34 bike shifting - output decimal in percent Emanuel Berg
@ 2019-05-19  3:50 ` Drew Adams
  2019-05-19  3:57   ` Drew Adams
  2019-05-19  7:05   ` Emanuel Berg
  0 siblings, 2 replies; 12+ messages in thread
From: Drew Adams @ 2019-05-19  3:50 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

> How do I output a decimal float digit
> 0 <= x <= 1 into percent? ("A percentage?"
> Correct English?)
> 
> But I wonder if/why-not there is/isn't
> a built-in way to do it?

Use function `format'.  See `C-h f format' and
(elisp) `Formatting Strings'.

E.g.
(setq foo  0.314159265358979)

(format "%2.14g%%" (* 100.0 foo))
 => "31.459265358979%"

(format "%2.2g%%" (* 100.0 foo))
 => "31%"



^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: bike shifting - output decimal in percent
  2019-05-19  3:50 ` Drew Adams
@ 2019-05-19  3:57   ` Drew Adams
  2019-05-19  4:09     ` Drew Adams
  2019-05-19  7:05   ` Emanuel Berg
  1 sibling, 1 reply; 12+ messages in thread
From: Drew Adams @ 2019-05-19  3:57 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

> Use function `format'.  See `C-h f format' and
> (elisp) `Formatting Strings'.

BTW, Common Lisp has an even more formidable
`format' - a veritable language unto itself.

https://www.cs.cmu.edu/Groups/AI/html/cltl/clm/node200.html

https://en.wikipedia.org/wiki/Format_(Common_Lisp)

http://www.lispworks.com/documentation/lw50/CLHS/Body/f_format.htm



^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: bike shifting - output decimal in percent
  2019-05-19  3:57   ` Drew Adams
@ 2019-05-19  4:09     ` Drew Adams
  0 siblings, 0 replies; 12+ messages in thread
From: Drew Adams @ 2019-05-19  4:09 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

> BTW, Common Lisp has an even more formidable
> `format' - a veritable language unto itself.

Oh, and this page has examples and explanation:

http://www.gigamonkeys.com/book/a-few-format-recipes.html



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: bike shifting - output decimal in percent
  2019-05-19  3:50 ` Drew Adams
  2019-05-19  3:57   ` Drew Adams
@ 2019-05-19  7:05   ` Emanuel Berg
  2019-05-19  7:35     ` Emanuel Berg
  2019-05-19 14:15     ` Drew Adams
  1 sibling, 2 replies; 12+ messages in thread
From: Emanuel Berg @ 2019-05-19  7:05 UTC (permalink / raw)
  To: help-gnu-emacs

Drew Adams wrote:

>> How do I output a decimal float digit0 <=
>> x <= 1 into percent? ("A percentage?"
>> Correct English?) [...]
>> 
>> But I wonder if/why-not there is/isn't
>> a built-in way to do it?
>
> Use function `format'. See `C-h f format' and
> (elisp) `Formatting Strings'.

You mean

    (info "(elisp) Formatting Strings")

?

> (format "%2.2g%%" (* 100.0 foo)) => "31%"

Great, thanks a lot :)

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: bike shifting - output decimal in percent
  2019-05-19  7:05   ` Emanuel Berg
@ 2019-05-19  7:35     ` Emanuel Berg
  2019-05-19  7:53       ` Tomas Nordin
  2019-05-19 14:26       ` Drew Adams
  2019-05-19 14:15     ` Drew Adams
  1 sibling, 2 replies; 12+ messages in thread
From: Emanuel Berg @ 2019-05-19  7:35 UTC (permalink / raw)
  To: help-gnu-emacs

>> (format "%2.2g%%" (* 100.0 foo)) => "31%"
>
> Great, thanks a lot :)

Oups, spoke (no pun intended :)) too soon.
Too much respect for Drew, I suppose ;)

But to be honest, the original program is
_much_ better. Check out the examples below:

;; secret bonus program :)
;; thanks to friend at #emacs
(defun percent-string (decimal)
  (concat
   (number-to-string
    (round (* 100 decimal)) ) "%") )
;; test: (percent-string 0.42857142857142855) ; "43%"

(defun bike-compute-step (from to)
  (percent-string
   (/ (- from to)
      from
      1.0) ))
;; test - should be 43% for
;; one 42t and one 24t chainring
;; according to an article in the spring 2019 issue
;; of "Bicycle Quarterly" (ISSN 1941-8809) -
;; (bike-compute-step 42 24) ; "43%"

;; (percent-string 0.01)  ; "1%"
;; (percent-string 0.1)   ; "10%"
;; (percent-string 1)     ; "100%
;; (percent-string 2)     ; "200%"

;; (format "%2.2g%%" (* 100.0 0.01)) ; " 1%"    (?)
;; (format "%2.2g%%" (* 100.0 0.1))  ; "10%"
;; (format "%2.2g%%" (* 100.0 1))    ; "1e+02%" (!?)
;; (format "%2.2g%%" (* 100.0 2))    ; "2e+02%" (...)


File: https://dataswamp.org/~incal/emacs-init/bike.el

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: bike shifting - output decimal in percent
  2019-05-19  7:35     ` Emanuel Berg
@ 2019-05-19  7:53       ` Tomas Nordin
  2019-05-19  8:02         ` Emanuel Berg
  2019-05-19 14:26       ` Drew Adams
  1 sibling, 1 reply; 12+ messages in thread
From: Tomas Nordin @ 2019-05-19  7:53 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

Emanuel Berg <moasenwood@zoho.eu> writes:

>>> (format "%2.2g%%" (* 100.0 foo)) => "31%"
>>
>> Great, thanks a lot :)
>
> Oups, spoke (no pun intended :)) too soon.
> Too much respect for Drew, I suppose ;)
>
> But to be honest, the original program is
> _much_ better. Check out the examples below:
>
> ;; secret bonus program :)
> ;; thanks to friend at #emacs
> (defun percent-string (decimal)
>   (concat
>    (number-to-string
>     (round (* 100 decimal)) ) "%") )
> ;; test: (percent-string 0.42857142857142855) ; "43%"
>
> (defun bike-compute-step (from to)
>   (percent-string
>    (/ (- from to)
>       from
>       1.0) ))
> ;; test - should be 43% for
> ;; one 42t and one 24t chainring
> ;; according to an article in the spring 2019 issue
> ;; of "Bicycle Quarterly" (ISSN 1941-8809) -
> ;; (bike-compute-step 42 24) ; "43%"
>
> ;; (percent-string 0.01)  ; "1%"
> ;; (percent-string 0.1)   ; "10%"
> ;; (percent-string 1)     ; "100%
> ;; (percent-string 2)     ; "200%"
>
> ;; (format "%2.2g%%" (* 100.0 0.01)) ; " 1%"    (?)
> ;; (format "%2.2g%%" (* 100.0 0.1))  ; "10%"
> ;; (format "%2.2g%%" (* 100.0 1))    ; "1e+02%" (!?)
> ;; (format "%2.2g%%" (* 100.0 2))    ; "2e+02%" (...)

Why not just use good old %f?

(format "%.0f%%" (* 0.127 100)) ;; -> "13%"

Or respecting SI recommendations:

(format "%.0f %%" (* 0.127 100)) ;; -> "13 %"



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: bike shifting - output decimal in percent
  2019-05-19  7:53       ` Tomas Nordin
@ 2019-05-19  8:02         ` Emanuel Berg
  2019-05-19  8:15           ` Tomas Nordin
  0 siblings, 1 reply; 12+ messages in thread
From: Emanuel Berg @ 2019-05-19  8:02 UTC (permalink / raw)
  To: help-gnu-emacs

Tomas Nordin wrote:

> Why not just use good old %f?
>
> (format "%.0f%%" (* 0.127 100)) ;; -> "13%"

Congrats, it works: 

;; (format "%.0f%%" (* 0.01 100)) ; "1%"
;; (format "%.0f%%" (* 0.1  100)) ; "10%"
;; (format "%.0f%%" (* 1    100)) ; "100%"
;; (format "%.0f%%" (* 2    100)) ; "200%"

> Or respecting SI recommendations:
>
> (format "%.0f %%" (* 0.127 100)) ;; -> "13 %"

Maybe if DIN recommended the same thing...

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: bike shifting - output decimal in percent
  2019-05-19  8:02         ` Emanuel Berg
@ 2019-05-19  8:15           ` Tomas Nordin
  2019-05-19  8:26             ` Emanuel Berg
  0 siblings, 1 reply; 12+ messages in thread
From: Tomas Nordin @ 2019-05-19  8:15 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

Emanuel Berg <moasenwood@zoho.eu> writes:

> Tomas Nordin wrote:
>
>> Why not just use good old %f?
>>
>> (format "%.0f%%" (* 0.127 100)) ;; -> "13%"
>
> Congrats, it works: 
>
> ;; (format "%.0f%%" (* 0.01 100)) ; "1%"
> ;; (format "%.0f%%" (* 0.1  100)) ; "10%"
> ;; (format "%.0f%%" (* 1    100)) ; "100%"
> ;; (format "%.0f%%" (* 2    100)) ; "200%"
>
>> Or respecting SI recommendations:
>>
>> (format "%.0f %%" (* 0.127 100)) ;; -> "13 %"
>
> Maybe if DIN recommended the same thing...

Straigh from the almighty Wikipedia:

"In German, the space is prescribed by the regulatory body in the
national standard DIN 5008."

https://en.wikipedia.org/wiki/Percent_sign

>
> --
> underground experts united
> http://user.it.uu.se/~embe8573
> https://dataswamp.org/~incal



^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: bike shifting - output decimal in percent
  2019-05-19  8:15           ` Tomas Nordin
@ 2019-05-19  8:26             ` Emanuel Berg
  0 siblings, 0 replies; 12+ messages in thread
From: Emanuel Berg @ 2019-05-19  8:26 UTC (permalink / raw)
  To: help-gnu-emacs

Tomas Nordin wrote:

>>> Or respecting SI recommendations:
>>>
>>> (format "%.0f %%" (* 0.127 100)) ;; -> "13 %"
>>
>> Maybe if DIN recommended the same thing...
>
> Straigh from the almighty Wikipedia:
>
> "In German, the space is prescribed by the
> regulatory body in the national standard
> DIN 5008."
>
> https://en.wikipedia.org/wiki/Percent_sign

Ha! Respect!

-- 
underground experts united
http://user.it.uu.se/~embe8573
https://dataswamp.org/~incal




^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: bike shifting - output decimal in percent
  2019-05-19  7:05   ` Emanuel Berg
  2019-05-19  7:35     ` Emanuel Berg
@ 2019-05-19 14:15     ` Drew Adams
  1 sibling, 0 replies; 12+ messages in thread
From: Drew Adams @ 2019-05-19 14:15 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

> > See (elisp) `Formatting Strings'.
> You mean (info "(elisp) Formatting Strings")?

Yes.



^ permalink raw reply	[flat|nested] 12+ messages in thread

* RE: bike shifting - output decimal in percent
  2019-05-19  7:35     ` Emanuel Berg
  2019-05-19  7:53       ` Tomas Nordin
@ 2019-05-19 14:26       ` Drew Adams
  1 sibling, 0 replies; 12+ messages in thread
From: Drew Adams @ 2019-05-19 14:26 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs

> the original program is _much_ better:
> 
> ;; test - should be 43% for
> ;; (bike-compute-step 42 24) ; "43%"
> 
> ;; (percent-string 0.01)  ; "1%"
> ;; (percent-string 0.1)   ; "10%"
> ;; (percent-string 1)     ; "100%
> ;; (percent-string 2)     ; "200%"
> 
> ;; (format "%2.2g%%" (* 100.0 0.01)) ; " 1%"    (?)
> ;; (format "%2.2g%%" (* 100.0 0.1))  ; "10%"
> ;; (format "%2.2g%%" (* 100.0 1))    ; "1e+02%" (!?)
> ;; (format "%2.2g%%" (* 100.0 2))    ; "2e+02%" (...)

It all depends on what you want and what your expected
input is.

(format "%1.1d%%" (* 100.0 0.01)) ; "1%"
(format "%1.1d%%" (* 100.0 0.1))  ; "10%"
(format "%1.1d%%" (* 100.0 1))    ; "100%"
(format "%1.1d%%" (* 100.0 2))    ; "200%"
(format "%1.1d%%" (round (* 100.0 0.42857142857142855))) ; "43%"

(defun percent-string (decimal)
  (format "%2.1d%%" (round (* 100.0 decimal))))

Like I said (regarding CL `format', but the same is true
of Emacs-Lisp `format': it's a language unto itself.



^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2019-05-19 14:26 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-05-19  1:34 bike shifting - output decimal in percent Emanuel Berg
2019-05-19  3:50 ` Drew Adams
2019-05-19  3:57   ` Drew Adams
2019-05-19  4:09     ` Drew Adams
2019-05-19  7:05   ` Emanuel Berg
2019-05-19  7:35     ` Emanuel Berg
2019-05-19  7:53       ` Tomas Nordin
2019-05-19  8:02         ` Emanuel Berg
2019-05-19  8:15           ` Tomas Nordin
2019-05-19  8:26             ` Emanuel Berg
2019-05-19 14:26       ` Drew Adams
2019-05-19 14:15     ` Drew Adams

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).