rounding off numbers

55 views
Skip to first unread message

coaxc...@gmail.com

unread,
Feb 10, 2021, 8:01:50 AM2/10/21
to DroidScript
Good day all

I have been out of the game for a while. My question is say, i have a number 25.345678 and i just want to return 25.345, what command would i use?

Steve Garman

unread,
Feb 10, 2021, 8:31:58 AM2/10/21
to DroidScript

function OnStart()
{
   lay=app.CreateLayout("linear", "VCenter, FillXY")
   txt=app.AddText(lay, "Hello")
   app.AddLayout(lay)
   
   num = 25.345678

   //to round it to 25.346
   r = num.toFixed( 3 )

   //to truncate it to 24.345
   t=(Math.floor(num*1000)/1000).toFixed( 3 )

   app.Quit( r + "\n" + t). // display both

Alan Hendry

unread,
Feb 10, 2021, 8:32:46 AM2/10/21
to DroidScript
Hi,
This looks like a javascript question.
Normally 25.345678 would round up to 25.346
The obvious answer is to multiply by 1000 ( 25345.678 ), then round ( 25346 ), then divide by 1000 (  25.346 )
var x =  25.345678  ;
var y = x * 1000 ;
var z = Math.round(y) ;
Alert (z / 1000) ;
If you just want to chop the extra decimal places then use Math.floor(y) instead of Math.round(y)
Regards, ah

Steve Garman

unread,
Feb 10, 2021, 8:34:17 AM2/10/21
to DroidScript
Oops!

The tuncate comment dhould read
//to truncate it to 25.345

Reply all
Reply to author
Forward
0 new messages