Is there a way for to round float values to the last number

Sorry about the title. It’s hard to explain this in a sentence. I’ll just give an example.
Is there a way I can get the intended output of: 0.05 and 0.3?

local firstDecimal = 0.05
local secondDecimal = 0.3

local str = string.format("%f  and %f", firstDecimal, secondDecimal)
--Intended output of: 0.05 and 0.3
1 Like

I’m not quite sure what the question means. What does getting the intended output mean?

When i run it and format, I get something like 0.05000000

local str = firstDecimal.." and "..secondDecimal

I guess you could try it without string.format

1 Like

Oh you can do math.round(firstDecimal *10)/10
and also math.round(firstSecond *100)/100

1 Like

no if you only want two decimal places using that method do math.floor not math.round

1 Like

math.round would be safer because sometimes u do end up with things like when its suppose to be 4 u get 3.999999999999

the problem is that then i print the value, it returns 0.05, but when i format it, it returns as 0.050000

local str = string.format("%0.2f  and %0.1f", firstDecimal, secondDecimal)
1 Like

think about this

so let’s say we only want 2 decimal place with 0.056

both methods we times the number by 100 which would 0.056 * 100 = 5.6
now here is where both methods are different
math.floor(5.6) = 5
math.round(5.6) = 6
now we divid by 100

5 / 100 = 0.05
6 / 100 = 0.06

so what have we learned here?

use math.floor if you want to completely remove the other decimal places
and use math.round if you want to remove the decimal places but also have the decimal rounded(example: 0.056 rounded to the hundredths place is 0.06)

my main point here is that both methods have their use cases, your way or my way are both the right ways to do it

1 Like

what itsLevande posted works, but Im not exactly sure what the .2 and .1 exactly do and I hope he can explain it a bit more

also
if you just wanted it to be a string this can also work

local firstDecimal = 0.05
local secondDecimal = 0.3

local str = string.format("%s  and %s", firstDecimal, secondDecimal)

They’re used for precision. 0.2 tells the program that I want it to format the string using two decimals of precision, while the 0.1 tells it that I only want one decimal of precision.

You can read more about this here.

1 Like

Oh that makes a lot of sense. Thats what I assumed at first, but when saw the variable firstDecimal and thought that was suppose to be something like .5 and the secondDecimal as .03 because of the variable names. but in the op its the other way around. Thanks!