另一个 C++ 输出对齐问题
Another C++ output alignment issue
过去 3 小时我一直在尝试对齐以下代码,但没有成功。有人可以告诉我我做错了什么吗?
我的目标是让字符串文字左对齐,变量右对齐,如下所示:
Loan amount: $ 10000.00
Monthly Interest Rate: 0.10%
但这就是我不断得到的:
Loan amount: $ 10000.00
Monthly Interest Rate: 0.10%
这是我一直在尝试的最新版本:
cout << setw(25) << "Loan amount:" << right << "$ "<< amount << endl;
cout << setw(25) << "Monthly Interest Rate:"<< right<< rateMonthly << "%" << endl;
非常感谢您的帮助。
这里是 live demo,它应该会输出您想要的内容。
std::to_string(double)
无法设置精度,这就是为什么我创建了一个小助手来完成它。
auto to_string_precision(double amount, int precision)
{
stringstream stream;
stream << fixed << setprecision(precision) << amount;
return stream.str();
};
cout << setw(25) << left << "Loan amount:" << setw(10) << right << ("$ " + to_string_precision(amount, 2)) << endl;
cout << setw(25) << left << "Monthly Interest Rate:" << setw(10) << right << (to_string_precision(rateMonthly, 2) + "%") << endl;
另类,我还是觉得这个更好看:
cout << setw(25) << left << "Loan amount:" << "$ " << amount << endl;
cout << setw(25) << left << "Monthly Interest Rate:" << rateMonthly << "%" << endl;
如果你想要
Loan amount: $ 10000.00
Monthly Interest Rate: 0.10%
如果你不想打扰左右你可以使用
cout << "Loan amount:" <<setw(25)<< "$ "<< amount << endl;
cout << "Monthly Interest Rate:"<< setw(19)<< rateMonthly << "%" << endl;
您可以使用以下
cout << setw(25) << left << "Loan amount:"<< "$ " << amount << endl;
cout << setw(28) << left << "Monthly Interest Rate:" << rateMonthly << "%" <<endl;
setw
字段宽度是为 next item to be output and is reset to 0 afterwards 定义的。这就是为什么只有文本显示在 25 个字符上而不是该行的剩余输出。
right
和 left
对齐符定义填充字符在字段中的放置位置。这意味着它仅适用于具有定义宽度的当前字段。这就是理由不适用于文本后面的项目的原因。
在这里你得到expected result:
cout <<setw(25)<< left<< "Loan amount:" << "$ "<< setw(10)<<right << amount << endl;
cout <<setw(25)<< left << "Monthly Interest Rate:"<<" "<<setw(10)<<right<< rateMonthly << " %" << endl;
如果您希望 $ 位于数字旁边,您必须确保将 $ 和数字串联成一个对象进行输出,方法是将它们放在一个字符串中,或者使用货币格式。
过去 3 小时我一直在尝试对齐以下代码,但没有成功。有人可以告诉我我做错了什么吗? 我的目标是让字符串文字左对齐,变量右对齐,如下所示:
Loan amount: $ 10000.00
Monthly Interest Rate: 0.10%
但这就是我不断得到的:
Loan amount: $ 10000.00
Monthly Interest Rate: 0.10%
这是我一直在尝试的最新版本:
cout << setw(25) << "Loan amount:" << right << "$ "<< amount << endl;
cout << setw(25) << "Monthly Interest Rate:"<< right<< rateMonthly << "%" << endl;
非常感谢您的帮助。
这里是 live demo,它应该会输出您想要的内容。
std::to_string(double)
无法设置精度,这就是为什么我创建了一个小助手来完成它。
auto to_string_precision(double amount, int precision)
{
stringstream stream;
stream << fixed << setprecision(precision) << amount;
return stream.str();
};
cout << setw(25) << left << "Loan amount:" << setw(10) << right << ("$ " + to_string_precision(amount, 2)) << endl;
cout << setw(25) << left << "Monthly Interest Rate:" << setw(10) << right << (to_string_precision(rateMonthly, 2) + "%") << endl;
另类,我还是觉得这个更好看:
cout << setw(25) << left << "Loan amount:" << "$ " << amount << endl;
cout << setw(25) << left << "Monthly Interest Rate:" << rateMonthly << "%" << endl;
如果你想要
Loan amount: $ 10000.00 Monthly Interest Rate: 0.10%
如果你不想打扰左右你可以使用
cout << "Loan amount:" <<setw(25)<< "$ "<< amount << endl;
cout << "Monthly Interest Rate:"<< setw(19)<< rateMonthly << "%" << endl;
您可以使用以下
cout << setw(25) << left << "Loan amount:"<< "$ " << amount << endl;
cout << setw(28) << left << "Monthly Interest Rate:" << rateMonthly << "%" <<endl;
setw
字段宽度是为 next item to be output and is reset to 0 afterwards 定义的。这就是为什么只有文本显示在 25 个字符上而不是该行的剩余输出。
right
和 left
对齐符定义填充字符在字段中的放置位置。这意味着它仅适用于具有定义宽度的当前字段。这就是理由不适用于文本后面的项目的原因。
在这里你得到expected result:
cout <<setw(25)<< left<< "Loan amount:" << "$ "<< setw(10)<<right << amount << endl;
cout <<setw(25)<< left << "Monthly Interest Rate:"<<" "<<setw(10)<<right<< rateMonthly << " %" << endl;
如果您希望 $ 位于数字旁边,您必须确保将 $ 和数字串联成一个对象进行输出,方法是将它们放在一个字符串中,或者使用货币格式。