Recent Changes - Search:

Oktatás

* Programozás 2
  + feladatsor
  + C feladatsor
  + Python feladatsor
  + GitHub oldal

* Szkriptnyelvek
  + feladatsor
  + quick link

* Adator. prog.
  + feladatsor
  + quick link

Teaching

* Prog. for Data Sci.
  ◇ exercises
  ◇ quick link

teaching assets


Félévek

* 2025/26/1
* archívum


Linkek

* kalendárium
   - munkaszüneti napok '20
* tételsorok
* jegyzetek
* szakdolgozat / PhD
* ösztöndíjak
* certificates
* C lang.
* C++
* C#
* Clojure
* D lang.
* Java
* Nim
* Scala


[ edit | logout ]
[ sandbox | passwd ]

Prettify a number

(1) prettify a number
def main():
    n = 123456

    print(n)  # 123456

    print(f"{n:,}")  # 123,456
import std.stdio;
import std.format;

void main()
{
    int n = 123456;

    writeln(n); // 123456

    writefln("%,s", n); // 123,456

    string s = format("%,s", n);
    writeln(s); // 123,456
}

If you have a huge number as a string (for instance, you converted a BigInt to string), then you can use this manual method:

(2) manual solution
#!/usr/bin/env rdmd

import std.stdio;
import std.algorithm;

string pretty_number(const string s) pure
{
    char[] result;

    auto i = 0;
    foreach_reverse (c; s)
    {
        if (i % 3 == 0 && i > 0)
        {
            result ~= ",";
        }
        result ~= c;
        ++i;
    }
    result.reverse();

    return result.idup;
}

void main()
{
    writeln(pretty_number("123456789")); // 123,456,789
}
Cloud City

  

Blogjaim, hobbi projektjeim

* The Ubuntu Incident
* Python Adventures
* @GitHub
* heroku
* extra
* haladó Python
* YouTube listák


Debrecen | la France


[ edit ]

Edit - History - Print *** Report - Recent Changes - Search
Page last modified on 2025 August 31, 19:24