How to convert number to string for example 1 to "One"

How to convert number to string for example 1 to “One”

I’ve used tostring and type, wondering what the best solution is?

This is a complicated question as you would need to create your own custom algorithm to interpret a number. A better system would be to abbreviate a number like 1200 to 1.2k instead of ‘one thousand-two hundred’

Simple number abbreviation script I just wrote and tested.

function abbreviate(number)
	if number < 1000 then
		return number
	else
		local abbreviations = {
			[3] = "K",
			[6] = "M",
			[9] = "B",
			[12] = "T",
			[15] = "Qa",
			[18] = "Qi"
		}
		local cleanNumber = math.floor(number)
		local length = #tostring(cleanNumber)
		local trueLength = length % 3
		if trueLength == 0 then
			trueLength = 3
		end
		local suffix
		if abbreviations[length-trueLength] then
			suffix = abbreviations[length-trueLength]
		else
			return number
		end
		return string.sub(tostring(cleanNumber),1,trueLength).."."..string.sub(tostring(cleanNumber),trueLength+1,trueLength+3).." "..suffix
	end
end
1 Like

you can use an external API and send an HttpRequest to convert number to a string. Here is the code:

local HttpService = game:GetService("HttpService")

local requestURL = "https://www.dataaccess.com/webservicesserver/numberconversion.wso/NumberToWords/JSON/debug?ubiNum="

function GetStringFromNumber(numToConvert)
	local result
	
	local success,res = pcall(function()		
		result = HttpService:GetAsync(requestURL..tostring(numToConvert))
	end)
	
	if success then
		return result
	else
		warn("Request failed!")
	end
	
end

print(GetStringFromNumber(999)) -- Replace 999 with the number you want to convert

(Make sure Http Requests are enabled under the game settings)

4 Likes

I like the idea of this but this is not a true solution as not only does this need to be called from the server, it also has its limits on how fast and often it can be called, and it has a ping delay from the roblox server to the api’s server. Also the API may not be 100% up and could be shutdown and ruin in game mechanics.

I completely agree, however this to my knowledge isn’t possible unless the OP creates a huge dictionary containing the words and their respective string.

1 Like

Thats not necessarily true. All you would need to do is have the name of each base number(1,2,3…) and the names of different powers(like hundred, thousand) and then do some arithmetic to combine all of it together.

I think you might have misread what the OP is trying to do. They are trying to convert a number, say 10 into a string “Ten” and they are not trying to abbreviate the number.

1 Like

did you not see what I said before that? I said that it would be difficult and that abbreviation is better and is much easier to do.

My bad, I didn’t read it properly.

I think that using an API is possible if the OP optimizes the code since the limit is 500 requests every minute per server, which is a reasonable amount.

1 Like

I don’t think you need to have large dictionary for it. However, it would be quite tedious to reinvent the wheel.
If you were going to have and convert a number to a string you will need to combine the tens and then the amount then simply make an exception for 10 - 19

1 Like

I know that 500 requests per minute is fine but there are many downfalls that really make it slow. (say we want to get from the client). First we must send a remote function to the server with our number, so the first delay is the client to server so lets say 60 ms ping, then it must interpret that, then send to the API, adding the ping of the server to the API and then the API to the server, say 150 ms, then it must take the result and send back to the client, another 60 ms ping and this is if nothing breaks or glitches. A 270 ms delay just to get your result back.

1 Like

500 requests per minute is how much Roblox allows, this doesn’t mean the address you’re using allows you to send 500 requests or simply refuses to give the wanted return if you do it more than they would like. You can also not trust that the delay is consistent, not just from Roblox’s side, but from the address/api’s side as well. I’ve experienced with various api’s that high traffic has caused high delays in response and even not wanting to respond if you’re also sending lots of requests on top of it.

Yes that’s what I am saying. I was saying that using a API for something like this scenario makes ZERO sense.

1 Like