Truncate Variable Length

Hello, I am writing a calculation program that will output a variable as such:

1305.5673076923

How I can trunc that variable to only include the first 2 decimal places? possibly even rounding up to the nearest full number would work. Thanks!

Sean [import]uid: 157382 topic_id: 30251 reply_id: 330251[/import]

As far as i know there is not formatting to the a wanted number of decimals.

What i did was the following:

[code]
local thenumber --1305.5673076923

local truncnumber = math.round(thenumber*100)/100 --the multiply/divide with 100 will make it round with 2 decimals, 10 would do 1 decimal and not multiplying with any number will round up to no decimal points.
[/code] [import]uid: 162464 topic_id: 30251 reply_id: 121167[/import]

Mike470, ah yes, that would work to.

I guess the method to use now depends on what ‘se’ wants to do with variable/numbers. [import]uid: 162464 topic_id: 30251 reply_id: 121171[/import]

Zwonkie - that would work but it changes the var itself. If you just want to format the display of the variable’s value, without changing the variable itself, you can do something like

string.format("%1.2f",thenumber) [import]uid: 160496 topic_id: 30251 reply_id: 121169[/import]

Hey Mike, I haven’t been able to get your solution to work. Here is the code:

actCurRatTot = 123.456789  
actCurRatTot = tostring( actCurRatTot )  
string.format("%1.2f",actCurRatTot)  
actCurRat = display.newText( "Actual Current Ratio: " .. actCurRatTot, 0, 0 )  

However, I was able to get the code that Zwonkie provided to round the number up to the nearest hundredth.

actCurRatTot = 123.456789  
actCurRatTot = math.round(actCurRatTot\*100)/100  
actCurRat = display.newText( "Actual Current Ratio: " .. actCurRatTot, 0, 0 )  

Did I miss something in the code for the string.format version?

Sean [import]uid: 157382 topic_id: 30251 reply_id: 121387[/import]

actCurRatTot = 123.456789
actCurRatTot=string.format("%1.2f",actCurRatTot)
actCurRat = display.newText( "Actual Current Ratio: " … actCurRatTot, 0, 0, native.systemFont,20 ) [import]uid: 160496 topic_id: 30251 reply_id: 121392[/import]

As far as i know there is not formatting to the a wanted number of decimals.

What i did was the following:

[code]
local thenumber --1305.5673076923

local truncnumber = math.round(thenumber*100)/100 --the multiply/divide with 100 will make it round with 2 decimals, 10 would do 1 decimal and not multiplying with any number will round up to no decimal points.
[/code] [import]uid: 162464 topic_id: 30251 reply_id: 121167[/import]

Mike470, ah yes, that would work to.

I guess the method to use now depends on what ‘se’ wants to do with variable/numbers. [import]uid: 162464 topic_id: 30251 reply_id: 121171[/import]

Zwonkie - that would work but it changes the var itself. If you just want to format the display of the variable’s value, without changing the variable itself, you can do something like

string.format("%1.2f",thenumber) [import]uid: 160496 topic_id: 30251 reply_id: 121169[/import]

Hey Mike, I haven’t been able to get your solution to work. Here is the code:

actCurRatTot = 123.456789  
actCurRatTot = tostring( actCurRatTot )  
string.format("%1.2f",actCurRatTot)  
actCurRat = display.newText( "Actual Current Ratio: " .. actCurRatTot, 0, 0 )  

However, I was able to get the code that Zwonkie provided to round the number up to the nearest hundredth.

actCurRatTot = 123.456789  
actCurRatTot = math.round(actCurRatTot\*100)/100  
actCurRat = display.newText( "Actual Current Ratio: " .. actCurRatTot, 0, 0 )  

Did I miss something in the code for the string.format version?

Sean [import]uid: 157382 topic_id: 30251 reply_id: 121387[/import]

actCurRatTot = 123.456789
actCurRatTot=string.format("%1.2f",actCurRatTot)
actCurRat = display.newText( "Actual Current Ratio: " … actCurRatTot, 0, 0, native.systemFont,20 ) [import]uid: 160496 topic_id: 30251 reply_id: 121392[/import]