Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Thousands separator in a number string
Started by IgnacioIturra Nov 19 2010 03:28 PM

4 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

IgnacioIturra

[GLOBAL: userInfoPane.html]
IgnacioIturra
  • Contributor

  • 359 posts
  • Guests

I can't figure out how to format my text so my scores show up with a thousands separator. Any tips? string.format seems to be useless for that.
uid: 10835 topic_id: 3817 reply_id: 303817


[TOPIC: post.html]
#2

finnk

[GLOBAL: userInfoPane.html]
finnk
  • Contributor

  • 149 posts
  • Corona SDK

Try string.gmatch or string.gsub with regex.

string.gmatch:
http://developer.anscamobile.com/node/3200

string.gsub:
http://developer.anscamobile.com/node/3222

patterns:
http://developer.anscamobile.com/content/strings#Patterns
If you haven't used it before, it will take some tweaking to become accustomed to it.
uid: 11024 topic_id: 3817 reply_id: 11589


[TOPIC: post.html]
#3

finnk

[GLOBAL: userInfoPane.html]
finnk
  • Contributor

  • 149 posts
  • Corona SDK

Come to think of it, maybe that's unnecessary.

Since you're only dealing with something that only displays scores, you can check for the string length and then just split strings after every third digit from the right. Then recombine, display, or print them with a comma and so on.
uid: 11024 topic_id: 3817 reply_id: 11591


[TOPIC: post.html]
#4

finnk

[GLOBAL: userInfoPane.html]
finnk
  • Contributor

  • 149 posts
  • Corona SDK

Here, I put together this function.

--Can be whateverscore = 1030003print (fixScore(score))


function fixScore(score)		--Number of loops		loops = string.sub(((string.len(score)/3)), 1,1)		--Find out the leading characters		leadingChars = math.fmod (string.len(score), 3)		--Offset loop when there's no leading characters		if (leadingChars == 0) then			loops = loops - 1			reach = 3		elseif (leadingChars == 1) then			reach = 1		elseif (leadingChars == 2) then			reach = 2 		end		--Reverse because it's easier to count backwards		scoretemp = string.reverse(score)		scorenew = ""		--Add commas at interval		for i=1, loops, 1 do			scorenew = scorenew .. (string.sub (scoretemp, ((i*3)-2), i*3) .. "," )		end		--Recombine		scorenew = string.sub(score, 1, reach) .. string.reverse(scorenew)		--Send new value		return scorenew	end

uid: 11024 topic_id: 3817 reply_id: 11594


[TOPIC: post.html]
#5

IgnacioIturra

[GLOBAL: userInfoPane.html]
IgnacioIturra
  • Contributor

  • 359 posts
  • Guests

Thanks for the response, it'll make my life easier. I forgot to ask before, but in drawing software you can choose one color to fill the font and another for the outline. I'm not clear if you can do the outline in Corona. Is this supported?

I'm in dire need to give my fonts a black outline to make them pop, otherwise I'm gonna have to use bitmap fonts, which I was trying to avoid.
uid: 10835 topic_id: 3817 reply_id: 11619



[topic_controls]
[/topic_controls]