Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Human readable numbers [duplicate]

Is there an easy way and dynamic way to format numbers in a string to be human readable? For example turn 10000000000 into 10,000,000,000. I have seen this question but the answers are outdated and broken (the one with the example).

like image 430
Jared Mackey Avatar asked Jan 10 '16 23:01

Jared Mackey


1 Answers

Try this psuedo algorithm:

  1. Divide the string length by 3
  2. Round that down, and we'll call it x
  3. Loop over the string x times, going backwards:

    1. Get the string at x times 3 position, or index [(x times 3) - 1], we'll call it y.
    2. Replace y with "," + y
like image 187
Quill Avatar answered Sep 27 '22 23:09

Quill