Selecting only 3 first digits of an integer/decimal to display on GUI

Hello fellow developers!
I am currently working on a project where there is a server multiplier. This server multiplier basically helps everyone on the server by multiplying their cash revenue. The problem is, when you upgrade the server multiplier, if doesn’t show the real value. Let me explain:

image
First of all, you see that when someone upgrades the server multiplier, they are supposed to add 0.3 to the old and new values. (which displays in game).
After upgrading multiple times, it should, theoretically display a number with only 1 decimal or just an integer, but for some reasons, it makes something like this:
image

So, I want to make sure that it only displays 1 digit on the text, like 1.2 or 3.6, not 3.6999999999999…
Is there a way to do this and also to make that if it’s an integer, it doesn’t displays 1.0 or 2.0?
By the way, I checked the numbers in the OldValue and NewValue NumberValues and it shows a 1 digit number, but it doesn’t display it like it should. So the problem is maybe from the GUI?

Thank you! :slight_smile:

Use if math.floor(number) == number then to check if it is an integer

Its kinda simple

So we have this number 4.28472, multiply it by 1000, and you get 4284.72, round it and we have 4284, now divide it by 1000, and now we have the first three decimals, which are 4.284

script.Parent.Text = math.round(val * 1000) / 1000

I just realized, you only want something lile 4.2, just replace 1000 with 10

The problem is that the value doesn’t have multiple decimals, like here:
image

But for some reasons it displays like 1.039999999999
So even if I round it, it’s not changing anything.

Maybe you can try tostring(val)?

Okay, but I have no idea how to xD
Can you explain to me how it works? :slight_smile:

Which one do you want me to explain? The tostring()?
Can you show me the line where you set the text?

Also, you should move this into Help and Feedback - Scripting Support

1 Like

Yep, tostring()
Btw, here’s the script:

image

Short is a BigNum script, which changes numbers from like 1000000000 to 1B
image

Ok, basically how tostring() works is that, it can convert a number to a string.

tostring(5) -- "5"
tostring({}) -- table 0x0288981 (just some random numbers)
tonumber("1") -- 1
tonumber("w") -- nil

But I really don’t get why the rounding technique I gave you wouldn’t work.

It didn’t work because the value itself isn’t a long decimal. For example:
The value is 1.3
But it displays 1.39999999 on the GUI

So even if I round the value, which is already rounded because it’s 1.3, then the display looks the same.

If you do the rounding technique I gave you for 1.3, it should work

1.3 * 10 = 13
math.round(13) = 13
13 / 10 = 1.3

But if you use rounding technique on 1.399999 then it will give you 1.4, replace math.round with math.floor if you want it to be 1.3

No, you don’t understand what I mean, let me explain again…

The value, is 1.3
But the GUI shows something like 1.3000000000001 or 1.299999999999
Let’s say I round 1.3 (because it’s the real value, 1.3000000000001 isn’t)
1.3 * 10 = 13
math.round(13) = 13
13 / 10 = 1.3

So I get the same result as before: 1.3
And the GUI doesn’t change, it still shows 1.29999999999 and sometimes 1.300000000001

It might be because of the Short module, other than that, I dont really know any other solution.

1 Like

So you want the whole number and one decimal place of the value to be displayed?

If so, similarly like what @NinjaFurfante07 & @calvin_coco said, you could use math.floor and tostring(). Hence, you do:

script.Parent.Model.Values.SurfaceGui.ValuesText.Text = tostring(math.floor(script.Parent.OldValue.Value * 10) / 10) .. "x => " ..  tostring(math.floor(script.Parent.NewValue.Value * 10) / 10) .. "x"

However, the code works without tostring so either that code above or below works:

script.Parent.Model.Values.SurfaceGui.ValuesText.Text = math.floor(script.Parent.OldValue.Value * 10) / 10).. "x => " ..  math.floor(script.Parent.NewValue.Value * 10) / 10 .. "x"
2 Likes

Use string.format, like so :

string.format("%.1f", script.Parent.OldValue.Value)

Quick run down, string.format lets you format strings in various ways by passing in a key and some value.

A key of “%.” tells the function to read the string, up until it locates a period.

A key of “%.1” modifies the function slightly by also returning one place after the period.

A key of “%.1f” will have the string be interpreted as a floating point number, which will allow the returned string to be rounded, based on the number that is returned.

print(string.format("%.1f", 3.6999999999999)) -- output : 3.7
6 Likes

I had this problem in my game, where you would have a number such as 100, but instead it would display 99.999… To combat this, I used string.format(). In your case, with string.format, instead of making the 99.999 a whole number, you want to format it to a float value. Here’s an example:

local valueToFormat = 12345.6789 -- number to be formatted
local format = "%.3f" -- format you want to use, this will round off your float value to 3 decimal places

local newStringValue = string.format(format, valueToFormat) -- format the string to your liking

print(newStringValue)
-- output: 12345.679

To read more about string.format, look at the documentation here: string | Roblox Creator Documentation

Sorry for the late response, but I don’t think so… My short module works perfectly on all over values except these ones…

Thanks!! It worked perfectly like I wanted :slight_smile:

1 Like