Currency with Pence System

What do I want to achieve?
I have a Bank and Wallet system that saves money values. I have the cash and bank datastore working but I’m having issues with the pence bit e.g: £4.50

This is the the current value (With no pence on the end):
image

And this is what I want on my game.
image

+1p onto 99p should round it up to 1564 (Nearest pound)
image

What is the issue?
Every time I test a Rounding Script it has 3 numbers on the end. Like this:
image

It also only rounds up when there’s a 4th 9 on the end like this
image

What solutions have you tried so far?
I have tried changing the maths behind it but cant seem to get it working.

After that, you should include more details if you have any. Try to make your topic as descriptive as possible, so that it’s easier for people to help you!

this is the rounding script I currently have.

local RoundBankValue = plr.Valuestats.Bank.Value
		RoundBankValue *= 0.01
		RoundBankValue = math.floor(RoundBankValue)
		RoundBankValue = RoundBankValue / 0.01

Could anyone who knows how to use math help me out.

1 Like

I think that switching the division and the multiplication may help. The way you have it currently, it rounds to the nearest 100, instead of to the nearest 0.01 (pence)

local RoundBankValue = plr.Valuestats.Bank.Value
RoundBankValue *= 100
RoundBankValue = math.floor(RoundBankValue)
RoundBankValue /= 100

Thanks for your help but unfortunately it still does the same thing.

Was the code you used exactly what I put? It’s odd that it had no effect.

Also, is it necessary to have the value in the NumberValue be rounded, or is it possible to leave it exact then just format it later on?

Yes I put exactly what you put in.

Also make sure that you set the value of the original as the updated value

local RoundBankValue = plr.Valuestats.Bank.Value
RoundBankValue *= 100
RoundBankValue = math.floor(RoundBankValue)
RoundBankValue /= 100
plr.Valuestats.Bank.Value = RoundBankValue 

Can you please send numbers you are testing with, plus resend the code that you currently have, with any modifications made. I am testing out the code myself and am having no issues with it.

Sweet, its doing something!

However its rounding it down to .98 when i set the decimal to .99
image

2280 is a number im tesing with

Tested with that same number and
image
When it is multiplied by 100, it sees the new value as

228098.9999999997

AKA a floating point inprecision (to do with the representation of these numbers in binary.)
I’ll have a go at finding a work around.

edit: @Haystees where’s the fun in doing that when you can come up with a semi-complicated solution to floating point inprecisions!

Alternatively, you could just save it as a whole number internally and divide by 100 and show that to players.
So something that shows as a cost of £1.20, you could internally represent as just 120.

So how would that system work when there’s the ability to add and take money from users bank Surely saving it as a decimal number instead of a whole number would be more easier

Saving it as a decimal number WOULD make it a little bit easier, however it means you have to do with floating points.

(Regarding the fix im trying to come up with for the incorrect rounding - is it fine if something like 5.99999999 - bunch of 9s - is rounded up instead of down?)

yeah having a bunch on 9s should be fine for my system

Yeah, probably. Not sure why you didn’t just use math.round() instead.

Because that’s not what OP wanted. They want to use math.floor to round it, like how currency is rounded down IRL

Alright, from my minimal testing, it seems like this here works:

local RoundBankValue = plr.Valuestats.Bank.Value
RoundBankValue*= 100
if RoundBankValue % 0.0001 > 0.000099999 then -- Floating Point Inprecision Workaround
	RoundBankValue *= 1000
	RoundBankValue= math.round(RoundBankValue)
	RoundBankValue /= 1000
	RoundBankValue += 0.0001
	RoundBankValue = math.floor(RoundBankValue)
else
	RoundBankValue = math.floor(RoundBankValue)
end	
RoundBankValue /= 100
plr.Valuestats.Bank.Value = RoundBankValue 

It’s a little bit more complicated, but seems to function. If you want me to explain anything about it LMK

Upon testing it, it kind of works.

it rounds up then goes back down to 99p

image

Do you think there’s a way of making the bunch of 9s needed to round up smaller?

This will round a number to the nearest tenth

local Number = 1.5982
local RoundedNumber = math.round(Number* 100) / 100
-- output: 1.6

To change the amount of 9s, just change these values:
(enjoy looking at the code without formatting…)

if RoundBankValue % 0.0001 > 0.000099999 then
RoundBankValue *= 1000
RoundBankValue= math.round(RoundBankValue)
RoundBankValue /= 1000
RoundBankValue += 0.0001
RoundBankValue = math.floor(RoundBankValue)
else

What number are you having an issue with?

So i want to to round up on the 3rd 9 so where would i change that