0
我一直在努力讓自己的代碼正常工作。我需要編寫一個程序,使用不超過12位數字的數字,並使用字符串操作將其轉換爲貨幣格式。我覺得我擁有大部分內容,但是我不知道如何讓 - 符號在$符號前面,以及在末尾沒有小數時如何讓逗號生效。C++中的貨幣格式化
這就是:
/*
I need a 12 digit maximum double precision number
verify that it is a double < 12 digits
verify that all digits are numeric with a possible
minus sign in the first position t
decimal is 2nd or 3rd last digit;
convert the number to currency using string manipulation
with commas placed at 3 digit intervals counted from the decimal
$ in front
- in front of $
0 is a positive number
Display currency format
*/
#include <iostream>
#include <string>
using namespace std;
void dollarFormat(string &);
int main()
{
cout << "Student Name: \t Chad Weireter" << endl
<< "Student ID:900658044" << endl << endl;
string input;
cout << "Please enter a number up to 12 digits long." << endl
<< "The number may be positive or negative" << endl
<< "and may include fractions (Up to two decimal positions)" << endl
<< "Sign and decimal dot(.) are not included in the digit count: ";
cin >> input;
dollarFormat(input);
cout << "The currency value is:\t" << input << endl;
cin.get();
cin.ignore();
}
void dollarFormat(string ¤cy)
{
int dp;
dp = currency.find('.');
if (dp > 3)
{
for (int x = dp - 3; x > 0; x -= 3)
currency.insert(x, ",");
}
currency.insert(0, "$");
}
如果這是作業,請這麼說,以便人們可以幫助,而無需爲你做功課:) – Dan