Using Decimal Points??? HELP!

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
no1g8tor
Posts: 62
Joined: Fri Nov 23, 2007 3:01 pm

Using Decimal Points??? HELP!

Post by no1g8tor » Fri Feb 22, 2008 4:55 pm

I have a situation that involves times that need to be rounded to the nearest hundreth. xx.xx. I am using the following command...Put round (Speed,2) into rspeed... and it works the only problem is that if it is 20.999, it makes it 21 but I need it to say 21.00 with the .00.

I tried the setnumberformat in the scripts, but it messes with the repeat statements.

Can I set just a field with the format xxx.xx.

Thanks

Janschenkel
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 977
Joined: Sat Apr 08, 2006 7:47 am
Contact:

Post by Janschenkel » Fri Feb 22, 2008 7:29 pm

You have to trick the engine into formatting the decimal number as a string at the right time. The easiest way to do this is by concatenating the number with a string.
Here's some example code:

Code: Select all

on mouseUp
  set the numberFormat to "#.00"
  repeat with i = 1 to 5
    put i + random(100) / 100 into tRandom
    put round(tRandom) & "" into tRounded  --> concatenate here
    put tRounded & return after tList
  end repeat
  answer tList
end mouseUp
Hope this helped,

Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sat Feb 23, 2008 2:04 am

Hi Jan,

Shouldn't the following be sufficient...

Code: Select all

set the numberformat to "#.00"
put round(20.999)*1 into fld "Your Field"
or am I missing something?

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Mark Smith
Posts: 179
Joined: Sat Apr 08, 2006 11:08 pm
Contact:

Post by Mark Smith » Mon Feb 25, 2008 3:45 am

You might also want to check the 'format' function.

put format("%0.3d", someNumber)

should put xx.xxx into the message box.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Mon Feb 25, 2008 3:49 am

Hi Mark,

I believe OP wants "xx.xxx" to be converted to "xx.00".

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

no1g8tor
Posts: 62
Joined: Fri Nov 23, 2007 3:01 pm

Post by no1g8tor » Tue Feb 26, 2008 2:46 pm

I am sorry if I wasnt clear. Exactly what I want is as follows. I have 8 Numbers in a field hat have longer than 2 decimal points.

31.33333
32.45
45.44433
56.1222223
etc...

I need them to be rounded to the nearest .xx.

They should look like
31.33
32.45
45.44
56.12

i used the following
set the numberFormat to "#.00"
Put round (Speed,2) into rspeed

The only problem with this is that it is in a loop and when it starts back through the loop the second round it changes all the numbers to the x.xx format and then screws up the repeat statement.

could I just convert the numbers after they are placed in curent field?

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Tue Feb 26, 2008 2:52 pm

No1g8tor,

Please post your repeat loop.

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

no1g8tor
Posts: 62
Joined: Fri Nov 23, 2007 3:01 pm

Post by no1g8tor » Tue Feb 26, 2008 4:22 pm

I figured out a way around it.

I should have done this before. I was thinking outside the box when i shold have been inside it...lol.

I just converted the number to the xx.xx format in the original location before I moved it to the list.

Thank you all for your help.


This forum is GREAT.

Post Reply