Help converting numbers into pounds and pennies

Hello! I’m here looking for help so I can figure out how to convert my numbers into correct GBP format. So far, my code works as it converts long decimals into smaller 2 decimal place numbers.

Here’s my code:

function module.ToDecimalPlace(value)
    value *= 100
    value = math.floor(value)
    value = value / 100
    return value
end

Let’s say “value” in the parameters is 1400.91152

My code will successfully convert this decimal to 2 decimal places.

1400.911.52 ==> 1400.9

The problem being it ends with .9

I need my code to end it with .90 instead to format it correctly as currency.

Any help is appreciated, I don’t think this is a very difficult topic but do let me know if you have any confusions or questions!

I’m not sure I understand the problem. Could you please elaborate?

The returned value is already floored to two decimal places. 1400.91152 rounds to 1400.91. Except if the goal is to also add a zero, for example with 1400.901521400.90.

In that case, converting to string and formatting can help. Because you’re flooring, it’s the same as getting rid of the rest of the decimals.

local value = 1400.90152
value = string.format("%.2f", value)
print(type(tonumber(value)), value) --> number, 1400.90
1 Like

Thanks @waves5217 for your help. Your method has worked!

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.