473,396 Members | 1,853 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

how to formatting and printing comma just use argument which returned from function.

I would like to output values with commas which formatting like as below.
may i know if any idea to make this kind of program?

int total sum = 123456;
printf("Total sum: %d \n",comma_function(total_sum));

**result**

Total sum: 123,456
Jun 13 '18 #1
1 1213
weaknessforcats
9,208 Expert Mod 8TB
You can use an algorithm. I would write a function that does the reformatting and returns the formatted number. Then have the calling function display the formatted number. You don't want the display in the function because the function would now be doing more than one thing and this is a design no-no.

The function should have two arguments, the number and a pointer to the formatted result. This keeps the function out of the memory allocation business.

The algorithm is:
Is the number 0. If yes you are done.

I usually see this done when formatting money values $123,456.78. To do this your original number must be in pennies.

int data = 12345678;

cout << '$';

cout << data / 100000 << ','; //this is 123

data = data %100000; //removes the 100,000's. Remainder is 45678

cout << data / 100; //this is 456

data = data % 100; //removes the 100's. Remainder is 78. Anything less then 100 is pennies

cout << '.';

cout << data << endl;

You should now have $123,456.78.

Does this help?
Jun 13 '18 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: BekTek | last post by:
I have class that has member function that take two argument.. I'd like to use the member function to algorithm such as remove_if.. How can I bind that? class Foo{ void f(){ int a = 10;...
4
by: aling | last post by:
What's the rule of default argument of function in C++? I found that the default argument of function could not necessary be a constant value. Is it true? Previously I thought that the default...
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
3
by: moho | last post by:
Hi I'm getting some errors, xxx is not a function. I'm sending a function as an argument to a funtion: function s(x) { .... } function t(a,b,c)
1
by: gouse | last post by:
Create or replace function Test(strTable in text) return bigint as $$ declare intSum bigint; begin intSum:=(Select sum(salary) from strTable); return intSum; end; $$ language...
30
by: Adam | last post by:
Hi, I have a simple printf-like function: int print(const char *format, ...) { char buffer; va_list argptr; int i;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.