Skip to content

eugene-malashkin/numeral-qt

Repository files navigation

numeral-qt

Class NumeralFormat designed for formatting numbers. The simplest example:

using namespace NumeralQt;	// The library's namespace

// Format number 12345.67 with format string "0,0.0". 
// Format string "0,0.0" means "use thousands separator, one digit after dot)":
QString st = NumeralFormat::format(12345.67, "0,0.0"); // st == "12,345.7"

Usage

Just include "numeral-qt.pri" in your project (some *.pro file created in Qt Creator):

# Includes numeral-qt library (change path if needed):
include(../numeral-qt/numeral-qt.pri)

And then include numeral.h in your code:

#include <numeral.h>

Or you can just copy two files, numeral.h and numeral.cpp, in appropriate directory and use it without "numeral-qt.pri".

The library uses C++11.

Format string

The library was inspired by http://numeraljs.com/, but has a slightly different syntax.

You can define numeral format in one string named "format string". For instance, if you want to format number without thousands separator ("0") and with two digits after dot (".00"), you should use format string "0.00":

// Format number 12345.678 with format string "0.00"
QString st = NumeralFormat::format(12345.678, "0.00"); // st == "12345.68"

Here are more advanced examples. Format number with thousands separator ("0,0") and with two digits after dot (".00"):

QString st = NumeralFormat::format(12345.678, "0,0.00") // st == "12,345.68"

Format number with thousands separator ("0,0") and with necessary digits after dot in an amount from 2 to 4 (".00**"):

QString st = NumeralFormat::format(12345,       "0,0.00**") // st == "12,345.00"
QString st = NumeralFormat::format(12345.678,   "0,0.00**") // st == "12,345.678"
QString st = NumeralFormat::format(12345.65438, "0,0.00**") // st == "12,345.6544"

Format number with sign ("+"), without thousands separator ("0") and no digits after dot:

QString st = NumeralFormat::format(12345.678,	"+0") // st == "+12346"
QString st = NumeralFormat::format(-12345,		"+0") // st == "-12345"

Format number as percent ("%" at the end of format string) with two digits after dot, with sign ("+"), without thousands separator ("0"):

QString st = NumeralFormat::format(0.1234,		"+0.00%")		// st == "+12.34%"
QString st = NumeralFormat::format(-0.12345,	"+0.00%")		// st == "-12.35%"

Storing numeral format

You can use an instance of NumeralFormat class for storing numeral format.

// You can use default constructor, or copy of NumeralFormat, or from QString
NumeralFormat n("+0.00%");
// Set precision range (the count of digits after dot) between 3 and 4
n.setPrecisionRange(3, 4);

QString st = n.toString(0.1234); // st == "+12.340%"

Working with locale

You can use appropriate locale for formatting. Because of you can't change QLocale parameters, there is NumeralLocale class - a composition of QLocale and changeable group separator.

NumeralLocale nl(QLocale::C, " ");  // " " is group separator
QString st = 
	NumeralFormat::format(1234.5678, "0,0.*", "NaN", nl); // st == "1 234.6"

Or you can define default numeral locale once, and don't care about specifying it later:

NumeralFormat::setDefaultNumeralLocale(NumeralLocale(QLocale::C, " "));

// Later
QString st = NumeralFormat::format(1234.5678);  // st == "1 234.5678"

Working with NaN

You can format NaN as you want:

NumeralFormat::format(qQNaN(), "0.0", "-") // "-" (as we defined in third parameter)

Or you can define default NaN stub once, and don't care about specifying it later:

NumeralFormat::setDefaultNanStub("nOT a nUMBER");

// Later
QString st = NumeralFormat::format(qQNaN()); // st == "nOT a nUMBER"