LUA, modulo and "number has no integer representation"

Viewed 67

I'm newbie in LUA. I want to simple display some time vaule

string_buf = string.format ( "%dh %dmin" ,Mode[2]/60,Mode[2]%60 )

But my device still inform me that at this line "#2 number has no integer representation". I find info that problem is that modulo give float. So I try to change it for

a= math.floor(Mode[2]%60)
string_buf = string.format ( "%dh %dmin" ,Mode[2]/60,a )

Result are the same. If I try to display this value as print in console then I see nice integer value- no matter If I made floor or not. Device and simulation in fact not work when rest from divide is not equal to 0

1 Answers

Modulo result is always an integer. Your problem is Mode[2]/60.

You cannot use %d for a float value. For floats use %f.

So either convert Mode[2]/60 to an integer

string_buf = string.format ( "%dh %dmin" ,math.floor(Mode[2]/60+.5), Mode[2]%60 )

or using floor division // if you don't want to round

string_buf = string.format ( "%dh %dmin" ,Mode[2]//60+.5, Mode[2]%60 )

or use a float with 0 digits

string_buf = string.format ( "%.0fh %dmin" ,Mode[2]/60,Mode[2]%60 )
Related