1

In our application we have a string of digits (stored as a String) for a registration number (Australian Business Number or Australian Company Number, similar to a SSN), for example:

"98123123999"

When we display this information in our web application it would be nice to be able to reformat the digits to be more readable, e.g.:

98 123 123 999

In ruby the best solution I have so far is to:

  1. Check the length
  2. Format the string by using the slice operator: [a[0,2], a[2,3], a[5,3], a[8, 3]].join(" ")

However this smells and doesn't seem right.

Whats the best way to do something seemingly so simple?

Edit: I should note as well that the string of numbers could be prefixed with a 0 (e.g. "012345678901") so it isn't possible to parse it as an integer then use format strings.

5 Answers 5

4

use

a = "98123123999"
=> "98123123999" 
b = a.split('').insert(2, ' ').insert(6, ' ').insert(10, ' ')
=> "9", "8", " ", "1", "2", "3", " ", "1", "2", "3", " ", "9", "9", "9"] 
b = a.split('').insert(2, ' ').insert(6, ' ').insert(10, ' ').join
=> "98 123 123 999" 

or if u want the string to be modified

a = "98123123999"
=> "98123123999" 
b = a.insert(2, ' ').insert(6, ' ').insert(10, ' ')
=> "98 123 123 999" 
a
=> "98 123 123 999" 
Sign up to request clarification or add additional context in comments.

1 Comment

Thats perfect, and seems pretty efficient. Given the strings are transient modification is not an issue. Thanks!
4

For a more generic point of view, if you use rails, you can use number_with_delimiter(number, delimiter=",", separator=".").

Otherwise, maybe just write something based on its source code.

number.to_s.gsub!(/(\d)(?=(\d{3})+(?!\d))/, "\\1#{' '}")

>> 1234567890.to_s.gsub!(/(\d)(?=(\d{3})+(?!\d))/, "\\1#{' '}")
=> "1 234 567 890"

4 Comments

suppose i have a number 1234512345. i want it to be displayed as '12345 12345'. how can i use the number_with_delimiter method here. an example would be really good.
@surase.prasad Edited my answer. You just replace the 3 with the 5 and you are good to go.
Umm, any reason why you interpolate a space into a string instead just writing "\\1 "?
@MladenJablanović Just follower the rails source code. Did not try to improve or change it.
1

Try this:

def format_digit(s, acc="")
  if s.size > 3
    new_acc = s[-3..-1] + " " +acc
    format_digit(s[0...-3], new_acc)
  else
    s + " " + acc
  end
end

s = "98123123999"
puts format_digit(s)

Comments

1

Not sure this suits your needs cause the group of two digits comes in the end instead of the beginning...

"98123123999".scan(/.{1,3}/).join(' ') #=> "981 231 239 99"

Comments

0
str           = "098123123999"
formatted_arr = []

if str.length % 3 == 0
    (str.length/3).times { formatted_arr << str.slice!(0..2) }
else
    formatted_arr << str.slice!(0..str.length % 3 - 1)
    (str.length/3).times { formatted_arr << str.slice!(0..2) }
end

puts formatted_arr.join(" ") # 098 123 123 999

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.