格式化具有给定小数位数的数字 [英] Formatting numbers with a given number of decimal places

查看:74
本文介绍了格式化具有给定小数位数的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我,请给我以下问题的解决方案。我需要在星期一提交。


写一个名为format的全局函数,它使用给定的小数位数格式化数字。该函数接受double类型的强制性第一个参数(要格式化的数字)和第二个类型为integer的可选参数(小数位数),并返回包含格式化文本的字符串对象。如果缺少第二个参数,则格式应假定为两(2)个小数位。此功能不允许使用用户交互代码。

将下面显示的主函数写为函数测试。

展开 | 选择 | Wrap | 行号

解决方案


请取悦,请给我以下问题的解决方案。我需要在星期一提交。


写一个名为format的全局函数,它使用给定的小数位数格式化数字。该函数接受double类型的强制性第一个参数(要格式化的数字)和第二个类型为integer的可选参数(小数位数),并返回包含格式化文本的字符串对象。如果缺少第二个参数,则格式应假定为两(2)个小数位。此功能不允许使用用户交互代码。

将下面显示的主函数写为函数测试。

展开 | 选择 | Wrap | 行号


我写了全局函数,但我试图编译它,程序给出了错误,完整的程序代码是如下:

展开 | 选择 | 换行 | 行号


您是用C或C ++编写的吗?


Can somone please, please give me the solution for the following problem. I need to submit it on Monday.

Write a global function called format, which formats numbers with a given number of decimal places. The function accepts a compulsory first argument of type double (the number to be formatted) and a second optional argument of type integer (the number of decimal places) and returns a string object containing the formatted text. If the second argument is missing, the formatting should assume two (2) decimal places. No user-interaction code is allowed in this function.
Write the main function shown below as a test for your function.

Expand|Select|Wrap|Line Numbers

解决方案

Can somone please, please give me the solution for the following problem. I need to submit it on Monday.

Write a global function called format, which formats numbers with a given number of decimal places. The function accepts a compulsory first argument of type double (the number to be formatted) and a second optional argument of type integer (the number of decimal places) and returns a string object containing the formatted text. If the second argument is missing, the formatting should assume two (2) decimal places. No user-interaction code is allowed in this function.
Write the main function shown below as a test for your function.

Expand|Select|Wrap|Line Numbers


I wrote the global function but I tried to compile it the program is giving errors and the full program code is as follows:

Expand|Select|Wrap|Line Numbers


Are you writing this in C or C++?


这篇关于格式化具有给定小数位数的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆