query re. displaying floats

Sep 28, 2007 at 5:44pm

query re. displaying floats

hi

i have an interface built wth lcd on which i want to display float values which i’ve rounded to 2 decimal places – ie, 0.777000 becomes 0.780000, but i don’t want the extra zeros – i only want to display 0.78. anyone any idea on how i do this?

thanks

#33876
Sep 28, 2007 at 6:06pm

Quote: robin wrote on Fri, 28 September 2007 10:44
—————————————————-
> hi
>
> i have an interface built wth lcd on which i want to display float values which i’ve rounded to 2 decimal places – ie, 0.777000 becomes 0.780000, but i don’t want the extra zeros – i only want to display 0.78. anyone any idea on how i do this?
>
> thanks
—————————————————-

[sprintf %.2f]

Make sure that you keep the output of the sprintf as a symbol. Max will try to convert it to a float, if possible, and you’ll loose your formatting.

mz

#113372
Sep 28, 2007 at 6:09pm

brilliant…i’d just turned to sprintf, but hadn’t got so far as that.

thanks a lot

#113373
Sep 28, 2007 at 6:13pm

uh…actually, that doesn’t appear to work. it still gives 4 decimal places – if i feed it 0.12 i get 0.1200 out, even as a symbol

#113374
Sep 28, 2007 at 6:43pm

Works here…

[sprintf symout %.2f]

#113375
Sep 28, 2007 at 6:46pm

Quote: robin wrote on Fri, 28 September 2007 11:13
—————————————————-
> uh…actually, that doesn’t appear to work. it still gives 4 decimal places – if i feed it 0.12 i get 0.1200 out, even as a symbol
—————————————————-

Max is aggressive about converting… looks like LCD is changing it back to a float. If you add a blank character at the end it should work.

Try:

#P window setfont “Sans Serif” 9.;
#P window linecount 1;
#P newex 179 100 60 196617 loadmess 8;
#P newex 144 166 30 196617 t s b;
#P message 164 193 97 196617 clear , moveto 5 20;
#P user lcd 144 264 49 34 1 1 0 0 0;
#P newex 144 144 108 196617 sprintf write %.2f%c;
#P flonum 144 67 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P connect 5 0 1 1;
#P connect 0 0 1 0;
#P connect 1 0 4 0;
#P connect 4 1 3 0;
#P connect 4 0 2 0;
#P connect 3 0 2 0;
#P window clipboard copycount 6;

#113376
Sep 28, 2007 at 6:57pm

On 28 sept. 07, at 20:46, mzed wrote:

>
> Quote: robin wrote on Fri, 28 September 2007 11:13
> —————————————————-
>> uh…actually, that doesn’t appear to work. it still gives 4
>> decimal places – if i feed it 0.12 i get 0.1200 out, even as a symbol
> —————————————————-
>
> Max is aggressive about converting… looks like LCD is changing it
> back to a float. If you add a blank character at the end it should
> work.

It’s because you need to output a symbol from sprintf, and then
prepend by “write”.

ej

#P window setfont “Sans Serif” 9.;
#P window linecount 1;
#P newex 144 217 73 196617 prepend write;
#P newex 144 166 30 196617 t s b;
#P message 164 193 97 196617 clear , moveto 5 20;
#P user lcd 144 264 49 34 1 1 0 0 0;
#P newex 144 144 102 196617 sprintf symout %.2f;
#P flonum 144 67 35 9 0 0 0 3 0 0 0 221 221 221 222 222 222 0 0 0;
#P connect 5 0 2 0;
#P connect 4 0 5 0;
#P connect 1 0 4 0;
#P connect 0 0 1 0;
#P connect 3 0 2 0;
#P connect 4 1 3 0;
#P window clipboard copycount 6;

#113377
Sep 28, 2007 at 7:12pm

lovely. thanks guys.

#113378

You must be logged in to reply to this topic.