Limit math calculations to tenths position

I am a brand newbie. i am trying a calculation and results are correct but i am getting 10 digits to the right of the decimal. how do i limit my results to one digit to the right of the decimal?

i looked at all the math functions and found the math.round function but it rounds to the nears whole number.

Any ideas?

Thx,
J [import]uid: 136907 topic_id: 35551 reply_id: 335551[/import]

Pseudocode:

round(value \* 10) / 10  

Should do it. Multiplying by the number of zeros we need before rounding, then dividing them out of the integer result gets us to the right decimal place.

FYI: J, this is in fact a fairly general programming question, not Corona or even Lua specific. You can find lots of stuff like this already answered on places like stack exchange - saves waiting for the response! [import]uid: 87138 topic_id: 35551 reply_id: 141303[/import]

Adding on to what Kyle said, are you looking to just display in 10ths or do you actually need to trim the number before you do math with it?

For the first version, you’re just wanting to output it with 1 decimal place, you could use:

print(string.format("%0.1f", youFloatingPointNumber)) -- or  
someDisplayText.text = string.format("0.1f", yourNumberToTrim) -- or  
text = "You are running at " .. string.format("%0.1f",yourNumber) .. " mph"  

See: http://docs.coronalabs.com/api/library/string/format.html

or however you want.

Corona has a math.round() function to execute Kyle’s method. See:
http://docs.coronalabs.com/api/library/math/round.html [import]uid: 199310 topic_id: 35551 reply_id: 141329[/import]

Thanks Kyle and Rob for the replies.

Rob - I used the code below and it worked fantastic.

[code]string.format("%0.1f",yourNumber)
[import]uid: 136907 topic_id: 35551 reply_id: 141368[/import]

Pseudocode:

round(value \* 10) / 10  

Should do it. Multiplying by the number of zeros we need before rounding, then dividing them out of the integer result gets us to the right decimal place.

FYI: J, this is in fact a fairly general programming question, not Corona or even Lua specific. You can find lots of stuff like this already answered on places like stack exchange - saves waiting for the response! [import]uid: 87138 topic_id: 35551 reply_id: 141303[/import]

Adding on to what Kyle said, are you looking to just display in 10ths or do you actually need to trim the number before you do math with it?

For the first version, you’re just wanting to output it with 1 decimal place, you could use:

print(string.format("%0.1f", youFloatingPointNumber)) -- or  
someDisplayText.text = string.format("0.1f", yourNumberToTrim) -- or  
text = "You are running at " .. string.format("%0.1f",yourNumber) .. " mph"  

See: http://docs.coronalabs.com/api/library/string/format.html

or however you want.

Corona has a math.round() function to execute Kyle’s method. See:
http://docs.coronalabs.com/api/library/math/round.html [import]uid: 199310 topic_id: 35551 reply_id: 141329[/import]

Thanks Kyle and Rob for the replies.

Rob - I used the code below and it worked fantastic.

[code]string.format("%0.1f",yourNumber)
[import]uid: 136907 topic_id: 35551 reply_id: 141368[/import]