Informatics - Decimal Formatting Program (week 7)

Hi everyone, here we go again with informatics c++ program. This block is actually still related with the previous block. So, I recommend you read my previous blog first. 

As you can see there, the result of the calculation is quite confusing right? There's no such proper dots or coma to separate the numbers. Here, with this code program in c++, we can format the decimal and also make the numbers readable and tidier. I use code blocks to compile and run the program.

Attach is the syntax of the program:

Line 1: provides access to the std::locale class and related functionality, which allows us to handle locale-specific settings such as number formatting.

Line 2: allows the use of standard input/output streams, including std::cout for output.

Line 3: provides functions for manipulating input and output streams, such as setting precision and formatting.

Line 5: defines a new class comma_numpunct, which inherits from std::numpunct<char>. std::numpunct is a class used to define formatting conventions for numbers, such as thousands separators. <char> indicates that we are customizing the formatting for characters.

Line 7: specifies that the following member functions will be accessible by this class and its derived classes but not by external code directly. 

Line 8: overrides the base class function, defines the character used as the thousands separator. The function is marked as const, indicating it does not modify the class state.

Line 10: returns a comma , as the thousands separator.

Line 13: specifies the grouping rule, in example, how many digits should be grouped together before placing a separator.

Line 15: specifies the grouping rule. \03 means that digits should be grouped in sets of 3. 

Line 19: entry point of the program.

Line 22: creates a new locale object comma_locale, using the current locale (std::locale()) and a custom facet (the comma_numpunct object) that overrides number formatting with a comma as the thousands separator.

Line 24: any numbers printed to the standard output will use this custom locale with commas as the thousands separator.

Line 25: 
- std::setprecision(2) sets the precision to 2 decimal places.
- std::fixed forces the output to be in fixed-point notation (no scientific notation).
- 14555000000 is the number being printed to the output stream. It will be formatted with commas as the thousands separator. I use that number because in my previous blog, the future value for continues compounding is 1.4555e+010. You can change this number according to what you want.

Output
After we run the program, the output will be like the picture above. The comma formatting as the thousand's separator so the number look more readable and tidier. End that's all our discussion for this blog. Hope this will help you to formatting the decimal on your program. Good luck and see ya!

Komentar

Postingan populer dari blog ini

Informatics - Making a Main C++ Program to Run all the Calculator Program (week 11)

Informatics - Triangles (week 18)

Informatics - Making Minimarket Discount Program in C++ version 1 (Week 12)