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.
.png)
.png)
Komentar
Posting Komentar