字符串的两位数除法
Two digit division from string
我想划分一个字符串值,我得到。假设我有一组顾客。每个客户在一封信中都有几张纸。我有两个字符串变量:
sheetsPerCustomer和totalPagescustomer
当前是这样的:
Customer A:
sheetsPerCustomer = "01" totalPagescustomer "06" // page 1 of 3
sheetsPerCustomer = "02" totalPagescustomer "06" // page 2 of 3
sheetsPerCustomer = "03" totalPagescustomer "06" // page 3 of 3
我必须除以totalPagescustomer,因为总页数是3而不是6。它应该看起来像这样:
sheetsPerCustomer = "01" totalPagescustomer "03" // page 1 of 3
sheetsPerCustomer = "02" totalPagescustomer "03" // page 2 of 3
sheetsPerCustomer = "03" totalPagescustomer "03" // page 3 of 3
直接除法不起作用,因为如果我将字符串转换为int进行除法,"0"将丢失。我需要保持左边的总页数可以是10,20等,所以我需要两个数字。有办法存档吗?
避免麻烦,使用整数。它使你的意图更加清晰。如果你想使用类似于整型的东西,那么也许你应该使用整型。
如果你需要显示一个2位数的数字,你可以这样做:
std::cout << std::setfill('0') << std::setw(2) << sheetsPerCustomer << std::endl;
这里有一个类似的方法:
#include <iostream>
using namespace std;
int main()
{
char buf[16];
int j = 7;
sprintf(buf, "%02d", j);
cout << "Result is " << buf << endl;
return 0;
}
打印Result is 07
相关文章:
- 如何在C ++中替换整数的前两位数字?
- 如何输出计算到小数点后两位的中间器?
- 将两位数字转换为低内存表示的最快方法
- 如何在计算器中将大数字表示为小数点后两位C++
- 可视化 如何使用C++向右强制小数点后两位
- 四舍五入双小数点后两位
- 将浮点数限制为小数点后两位
- C++ 如何获取兰登数以及如何保存带有 ONY 两位小数的双精度
- 使用 "double" 输出具有两位小数的值
- 程序查找并打印第一个完全正方形 (i*i),其最后两位数字都是奇数
- 转换为十六进制并删除最后两位数字
- 我将如何在不使用 if 语句、字符数组、apstring 或 atoi 的情况下将带有两位数字的字符串转换为 int
- C++四舍五入小数点后两位
- x86上两个128位整数的高效乘法/除法(无64位)
- 字符串的两位数除法
- 如何正确格式化c++浮点数到小数点后两位
- 四舍五入浮点数:精确地输出小数点后的两位数字
- 我在试着编写一种疾病在不同年龄段的发病率,我需要把它写成一个百分数它的精度是小数点后两位
- 如何将双精度转换为小数点后两位
- 将字节转换为两位十六进制数字