From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Tomas Nordin Newsgroups: gmane.emacs.help Subject: Re: bike shifting - output decimal in percent Date: Sun, 19 May 2019 09:53:35 +0200 Message-ID: <877eamop6o.fsf@fliptop.i-did-not-set--mail-host-address--so-tickle-me> References: <86mujjrzw0.fsf@zoho.eu> <3b5af744-8dc6-42d0-9273-a8030e2b3443@default> <86blzysz4e.fsf@zoho.eu> <867eamsxq2.fsf@zoho.eu> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="57970"; mail-complaints-to="usenet@blaine.gmane.org" To: Emanuel Berg , help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Sun May 19 09:53:58 2019 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hSGdp-000EsH-Bq for geh-help-gnu-emacs@m.gmane.org; Sun, 19 May 2019 09:53:57 +0200 Original-Received: from localhost ([127.0.0.1]:45326 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hSGdo-0001Ol-9k for geh-help-gnu-emacs@m.gmane.org; Sun, 19 May 2019 03:53:56 -0400 Original-Received: from eggs.gnu.org ([209.51.188.92]:37358) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1hSGdZ-0001Of-6v for help-gnu-emacs@gnu.org; Sun, 19 May 2019 03:53:42 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1hSGdY-0001Uo-4G for help-gnu-emacs@gnu.org; Sun, 19 May 2019 03:53:41 -0400 Original-Received: from mout02.posteo.de ([185.67.36.66]:58461) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1hSGdX-0001U9-LA for help-gnu-emacs@gnu.org; Sun, 19 May 2019 03:53:40 -0400 Original-Received: from submission (posteo.de [89.146.220.130]) by mout02.posteo.de (Postfix) with ESMTPS id 70F5C2400E6 for ; Sun, 19 May 2019 09:53:36 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1558252416; bh=jnJ3/rBeADo71tCcAmJNklCWlW6DeyZpGiXRqGJ1GDc=; h=From:To:Subject:Date:From; b=T67+DBiko8aoUMDIQ8tWnpUqlwuvsHK9Vbp5uS2ghEV0EwrgksmooGsISscrLljA4 4uEGvbOf/vY0yltOqNe1TQse9LL12H7d0jheNGBDz1Am+s+iAguyJG1V7FuDtHDckO /M7JvZSkYu4EsyUJpoaJK7UxzZW3kxtqf3kdEe+0DpkDQ9n0kBlU2p6hVMjFMrrPQ6 Q++R+28qLh09AAHdyQwNVz+LgudzcO5b6HUi4tViDBI9zKxK1VQFBZ2oK2x6XmvEYl hZO8gJYhWZ+DkgXQqVkkPCZ8m9NPwCkEd3fN+yCOZU0BpVQeoXwy2GeNo/4o1nSmAz mI0FzwdehjBrQ== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 456Dmb4MHwz9rxT; Sun, 19 May 2019 09:53:35 +0200 (CEST) In-Reply-To: <867eamsxq2.fsf@zoho.eu> X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 185.67.36.66 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:120531 Archived-At: Emanuel Berg 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 %"