Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
COMMUNITY QUESTION

How do I round this up to three decimals?

local PAGE = script.Parent.Parent
local PlayerAge = PAGE.AccountAge
local PlayerName = PAGE.Name
local ageInMonths = (math.floor(PlayerAge * 1000) / 1000) / 30
local ageInYears = (math.floor(PlayerAge * 1000) / 1000) / 365

game.StarterGui:SetCore("ChatMakeSystemMessage",{Text = "Welcome "..PlayerName.." Your account age is "..PlayerAge.." days old",Color = Color3.new(50,150,0),Font = Enum.Font.ArialBold,FontSize = Enum.FontSize.Size32})
if PlayerAge < 30 then
	game.game.StarterGui:SetCore("ChatMakeSystemMessage",{Text = "Which isn't even a month. ",Color = Color3.new(50,150,0),Font = Enum.Font.ArialBold,FontSize = Enum.FontSize.Size32})
	else
end
if PlayerAge < 365 then
	game.StarterGui:SetCore("ChatMakeSystemMessage",{Text = "Which isn't even a year, But it is however "..ageInMonths.." months old.",Color = Color3.new(50,150,0),Font = Enum.Font.ArialBold,FontSize = Enum.FontSize.Size32})
	elseif 
    PlayerAge > 365 then
	game.StarterGui:SetCore("ChatMakeSystemMessage",{Text = "Which is "..ageInMonths.." in months".." or "..ageInYears.." in years",Color = Color3.new(50,150,0),Font = Enum.Font.ArialBold,FontSize = Enum.FontSize.Size32})
end

at the fifth and sixths line (ageInMonths) and (ageInYears) i want it so the output only leaves 3 decimals on each months and years because in game it does like 10 decimals, what do i do?

1 answer

You could limit the size of the float by using string patterns. You can read up on them here: https://developer.roblox.com/en-us/articles/string-patterns-reference, but essentially they allow you to intelligently manipulate strings. e.g. if we had something like 134245.13489235, we know that the string would be a series of integers, followed by a period, followed by another string of integers. Since we know that "%d" references a digit, and "+" references one or more of the former, the string pattern "%d+%.*%d?%d?%d?" will suffice. Written in code, this would be

string.match(string, "%d+%.*%d?%d?%d?")

However, this does pose another issue. How do we reliably convert this float to a string? Well, using string formatting, we can reliably convert your float to a properly formatted string. Here's an example:

local float -- Your number
local string = string.format("%f", float)

An alternative solution (if you only needed 2 decimals), would be to convert your float to a double using %d instead in the code box above, eliminating the need for the string.match. But, let's finish this up. Your final solution would be:

local float -- Your number
local converted = string.format("%f", float):match("%d+%.*%d?%d?%d?")

Of course this isn't properly rounded, but that's a whole other level of complicated I unfortunately cannot explain in a single message. If this answer was helpful, please mark as solution, thx c;

3 comments

0

i dont understand where i'm supposed to put PlayerAge

0

The float variable represents your number.

0

yeah but then where do i use converted?

This discussion is preserved as it was. New answers, comments, and votes are closed.