Commatizing numbers: Difference between revisions

Scala contribution added.
(Added Go)
(Scala contribution added.)
Line 811:
return _</lang>
'''output''' when using the internal strings for input:
<pre>before──►pi=3.14159265358979323846264338327950288419716939937510582097494459231
<pre>
before──►pi=3.14159265358979323846264338327950288419716939937510582097494459231
after──►pi=3.14159 26535 89793 23846 26433 83279 50288 41971 69399 37510 58209 74944 59231
 
Line 843 ⟶ 842:
 
before──►6/9/1946 was a good year for some.
after──►6/9/1946 was a good year for some.</pre>
=={{header|Scala}}==
</pre>
===Java-ish version===
<lang Scala>import java.io.File
import java.util.Scanner
import java.util.regex.Pattern
 
object CommatizingNumbers extends App {
 
def commatize(s: String): Unit = commatize(s, 0, 3, ",")
 
def commatize(s: String, start: Int, step: Int, ins: String): Unit = {
if (start >= 0 && start <= s.length && step >= 1 && step <= s.length) {
val m = Pattern.compile("([1-9][0-9]*)").matcher(s.substring(start))
val result = new StringBuffer(s.substring(0, start))
if (m.find) {
val sb = new StringBuilder(m.group(1)).reverse
for (i <- step until sb.length by step) sb.insert(i, ins)
m.appendReplacement(result, sb.reverse.toString)
}
println(m.appendTail(result))
}
}
 
commatize("pi=3.14159265358979323846264338327950288419716939937510582" + "097494459231", 6, 5, " ")
commatize("The author has two Z$100000000000000 Zimbabwe notes (100 " + "trillion).", 0, 3, ".")
 
val sc = new Scanner(new File("input.txt"))
while (sc.hasNext) commatize(sc.nextLine)
}</lang>
Anonymous user