ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* Real number with Lua
@ 2017-04-19 13:25 Fabrice Couvreur
  2017-04-19 14:04 ` Otared Kavian
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Fabrice Couvreur @ 2017-04-19 13:25 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 1185 bytes --]

Hello,
I do not know Lua but I'll learn later. How to display only 3 digits after
the decimal point ?
thank you,
Fabrice

\starttext
\startlinecorrection[blank]
      \startmidaligned
        \bTABLE[width=2cm,offset=0.8ex,align={lohi,middle}]
        \bTR
        \bTD \math{P} \eTD
        \bTD \math{m} \eTD
        \bTD \math{g_M} \eTD
        \eTR
        \bTR
        \bTD \math{1.6} \eTD
        \bTD \math{0.4} \eTD
        \bTD \startluacode context(1.6/0.4)\stopluacode \eTD
        \eTR
        \bTR
        \bTD \math{3.1} \eTD
        \bTD \math{0.8} \eTD
        \bTD \startluacode context(3.1/0.8)\stopluacode \eTD
        \eTR
        \bTR
        \bTD \math{4.3} \eTD
        \bTD \math{1.2} \eTD
        \bTD \startluacode
                context(4.3/1.2)
              \stopluacode
        \eTD
        \eTR
        \bTR
        \bTD \math{5.8} \eTD
        \bTD \math{1.6} \eTD
        \bTD \startluacode context(5.8/1.6)\stopluacode \eTD
        \eTR
        \bTR
        \bTD \math{7.5} \eTD
        \bTD \math{2} \eTD
        \bTD \startluacode context(7.5/2)\stopluacode \eTD
        \eTR
        \eTABLE
      \stopmidaligned
    \stoplinecorrection
\stoptext

[-- Attachment #1.2: Type: text/html, Size: 1835 bytes --]

[-- Attachment #2: Type: text/plain, Size: 492 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Real number with Lua
  2017-04-19 13:25 Real number with Lua Fabrice Couvreur
@ 2017-04-19 14:04 ` Otared Kavian
  2017-04-19 14:19   ` Fabrice Couvreur
  2017-04-19 14:22 ` Procházka Lukáš Ing.
  2017-04-19 14:23 ` Herbert Voss
  2 siblings, 1 reply; 6+ messages in thread
From: Otared Kavian @ 2017-04-19 14:04 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 2085 bytes --]

Hi Fabrice,

You can use a construction such as:

\starttext
\ctxlua{x = math.floor(10^3*math.pi)/10^3; context(x)}
\stoptext

Best regards: OK

> On 19 Apr 2017, at 15:25, Fabrice Couvreur <fabrice1.couvreur@gmail.com> wrote:
> 
> Hello,
> I do not know Lua but I'll learn later. How to display only 3 digits after the decimal point ?
> thank you,
> Fabrice
> 
> \starttext
> \startlinecorrection[blank]
>       \startmidaligned
>         \bTABLE[width=2cm,offset=0.8ex,align={lohi,middle}]
>         \bTR
>         \bTD \math{P} \eTD
>         \bTD \math{m} \eTD
>         \bTD \math{g_M} \eTD
>         \eTR
>         \bTR
>         \bTD \math{1.6} \eTD
>         \bTD \math{0.4} \eTD
>         \bTD \startluacode context(1.6/0.4)\stopluacode \eTD
>         \eTR
>         \bTR
>         \bTD \math{3.1} \eTD
>         \bTD \math{0.8} \eTD
>         \bTD \startluacode context(3.1/0.8)\stopluacode \eTD
>         \eTR
>         \bTR
>         \bTD \math{4.3} \eTD
>         \bTD \math{1.2} \eTD
>         \bTD \startluacode 
>                 context(4.3/1.2)
>               \stopluacode
>         \eTD
>         \eTR
>         \bTR
>         \bTD \math{5.8} \eTD
>         \bTD \math{1.6} \eTD
>         \bTD \startluacode context(5.8/1.6)\stopluacode \eTD
>         \eTR
>         \bTR
>         \bTD \math{7.5} \eTD
>         \bTD \math{2} \eTD
>         \bTD \startluacode context(7.5/2)\stopluacode \eTD
>         \eTR
>         \eTABLE
>       \stopmidaligned
>     \stoplinecorrection
> \stoptext
> ___________________________________________________________________________________
> If your question is of interest to others as well, please add an entry to the Wiki!
> 
> maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
> webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
> archive  : https://bitbucket.org/phg/context-mirror/commits/
> wiki     : http://contextgarden.net
> ___________________________________________________________________________________


[-- Attachment #1.2: Type: text/html, Size: 5192 bytes --]

[-- Attachment #2: Type: text/plain, Size: 492 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Real number with Lua
  2017-04-19 14:04 ` Otared Kavian
@ 2017-04-19 14:19   ` Fabrice Couvreur
  0 siblings, 0 replies; 6+ messages in thread
From: Fabrice Couvreur @ 2017-04-19 14:19 UTC (permalink / raw)
  To: mailing list for ConTeXt users


[-- Attachment #1.1: Type: text/plain, Size: 232 bytes --]

2017-04-19 16:04 GMT+02:00 Otared Kavian <otared@gmail.com>:

>
> \starttext
> \ctxlua{x = math.floor(10^3*math.pi)/10^3; context(x)}
> \stoptext
>


​Hi Kavian (or Otared ?),
​Thank you very much, it works.
Fabrice

[-- Attachment #1.2: Type: text/html, Size: 929 bytes --]

[-- Attachment #2: Type: text/plain, Size: 492 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Real number with Lua
  2017-04-19 13:25 Real number with Lua Fabrice Couvreur
  2017-04-19 14:04 ` Otared Kavian
@ 2017-04-19 14:22 ` Procházka Lukáš Ing.
  2017-04-19 17:31   ` Aditya Mahajan
  2017-04-19 14:23 ` Herbert Voss
  2 siblings, 1 reply; 6+ messages in thread
From: Procházka Lukáš Ing. @ 2017-04-19 14:22 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hello,

do you mean this? - It shows EXACTLY 3 digits after the decimal point:

----
\cldcontext{string.format("\letterpercent.3f", 5.8/1.6)}

\cldcontext{string.format(string.char(37) .. ".3f", 5.8/1.6)}

\startluacode
   context(string.format("%.3f", 5.8/1.6))
\stopluacode
----

Best regards,

Lukas


On Wed, 19 Apr 2017 15:25:07 +0200, Fabrice Couvreur <fabrice1.couvreur@gmail.com> wrote:

> Hello,
> I do not know Lua but I'll learn later. How to display only 3 digits after
> the decimal point ?
> thank you,
> Fabrice
>
> \starttext
> \startlinecorrection[blank]
>       \startmidaligned
>         \bTABLE[width=2cm,offset=0.8ex,align={lohi,middle}]
>         \bTR
>         \bTD \math{P} \eTD
>         \bTD \math{m} \eTD
>         \bTD \math{g_M} \eTD
>         \eTR
>         \bTR
>         \bTD \math{1.6} \eTD
>         \bTD \math{0.4} \eTD
>         \bTD \startluacode context(1.6/0.4)\stopluacode \eTD
>         \eTR
>         \bTR
>         \bTD \math{3.1} \eTD
>         \bTD \math{0.8} \eTD
>         \bTD \startluacode context(3.1/0.8)\stopluacode \eTD
>         \eTR
>         \bTR
>         \bTD \math{4.3} \eTD
>         \bTD \math{1.2} \eTD
>         \bTD \startluacode
>                 context(4.3/1.2)
>               \stopluacode
>         \eTD
>         \eTR
>         \bTR
>         \bTD \math{5.8} \eTD
>         \bTD \math{1.6} \eTD
>         \bTD \startluacode context(5.8/1.6)\stopluacode \eTD
>         \eTR
>         \bTR
>         \bTD \math{7.5} \eTD
>         \bTD \math{2} \eTD
>         \bTD \startluacode context(7.5/2)\stopluacode \eTD
>         \eTR
>         \eTABLE
>       \stopmidaligned
>     \stoplinecorrection
> \stoptext
>


-- 
Ing. Lukáš Procházka | mailto:LPr@pontex.cz
Pontex s. r. o.      | mailto:pontex@pontex.cz | http://www.pontex.cz | IDDS:nrpt3sn
Bezová 1658
147 14 Praha 4

Tel: +420 241 096 751 (+420 720 951 172)
Fax: +420 244 461 038

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Real number with Lua
  2017-04-19 13:25 Real number with Lua Fabrice Couvreur
  2017-04-19 14:04 ` Otared Kavian
  2017-04-19 14:22 ` Procházka Lukáš Ing.
@ 2017-04-19 14:23 ` Herbert Voss
  2 siblings, 0 replies; 6+ messages in thread
From: Herbert Voss @ 2017-04-19 14:23 UTC (permalink / raw)
  To: ntg-context

Am 19.04.2017 um 15:25 schrieb Fabrice Couvreur:

> I do not know Lua but I'll learn later. How to display only 3 digits
> after the decimal point ?

The default is 3 digits


\startluacode
function round(a,dec)
   local dec = dec or 3
   local b = math.pow(10,dec)
   return math.floor(a*b)/b
end
\stopluacode

\starttext
\startlinecorrection[blank]
\startmidaligned
\bTABLE[width=2cm,offset=0.8ex,align={lohi,middle}]
\bTR
\bTD \math{P} \eTD
\bTD \math{m} \eTD
\bTD \math{g_M} \eTD
\eTR
\bTR
\bTD \math{1.6} \eTD
\bTD \math{0.4} \eTD
\bTD \startluacode context(round(1.6/0.4,1))\stopluacode \eTD
\eTR
\bTR
\bTD \math{3.1} \eTD
\bTD \math{0.8} \eTD
\bTD \startluacode context(round(3.1/0.8))\stopluacode \eTD
\eTR
\bTR
\bTD \math{4.3} \eTD
\bTD \math{1.2} \eTD
\bTD \startluacode context(round(4.3/1.2,2)) \stopluacode
\eTD
\eTR
\bTR
\bTD \math{5.8} \eTD
\bTD \math{1.6} \eTD
\bTD \startluacode context(5.8/1.6)\stopluacode \eTD
\eTR
\bTR
\bTD \math{7.5} \eTD
\bTD \math{2} \eTD
\bTD \startluacode context(7.5/2)\stopluacode \eTD
\eTR
\eTABLE
\stopmidaligned
\stoplinecorrection
\stoptext


Herbert

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: Real number with Lua
  2017-04-19 14:22 ` Procházka Lukáš Ing.
@ 2017-04-19 17:31   ` Aditya Mahajan
  0 siblings, 0 replies; 6+ messages in thread
From: Aditya Mahajan @ 2017-04-19 17:31 UTC (permalink / raw)
  To: mailing list for ConTeXt users

[-- Attachment #1: Type: TEXT/PLAIN, Size: 543 bytes --]

On Wed, 19 Apr 2017, Procházka Lukáš Ing. wrote:

> Hello,
>
> do you mean this? - It shows EXACTLY 3 digits after the decimal point:
>
> ----
> \cldcontext{string.format("\letterpercent.3f", 5.8/1.6)}
>
> \cldcontext{string.format(string.char(37) .. ".3f", 5.8/1.6)}
>
> \startluacode
>   context(string.format("%.3f", 5.8/1.6))
> \stopluacode

\startluacode context("%0.3f", 5.8/1.6) \stopluacode

is simpler.

Also see: 
http://wiki.contextgarden.net/Programming_in_LuaTeX#Arithmetic_without_using_an_abacus

Aditya

[-- Attachment #2: Type: text/plain, Size: 492 bytes --]

___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

end of thread, other threads:[~2017-04-19 17:31 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-04-19 13:25 Real number with Lua Fabrice Couvreur
2017-04-19 14:04 ` Otared Kavian
2017-04-19 14:19   ` Fabrice Couvreur
2017-04-19 14:22 ` Procházka Lukáš Ing.
2017-04-19 17:31   ` Aditya Mahajan
2017-04-19 14:23 ` Herbert Voss

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 NNTP newsgroup(s).