2

C# 言語の初心者で、ローン住宅ローン計算機の作成を終えたばかりで、以下のコードのフォーマットに問題があります。私がやろうとしているのは、毎月の支払い値を小数点以下 2 桁にフォーマットし、「$」記号を追加することです。どんな助けでも大歓迎です。ありがとう!

元本金額の入力例:

//User input for Principle amount in dollars
Console.Write("Enter the loan amount, in dollars(0000.00): ");
principleInput = Console.ReadLine();
principle = double.Parse(principleInput);
//Prompt the user to reenter any illegal input
        if (principle < 0)
        {
            Console.WriteLine("The value for the mortgage cannot be a negative value");
            principle = 0;
        }



//Calculate the monthly payment

double loanM = (interest / 1200.0);
double numberMonths = years * 12;
double negNumberMonths = 0 - numberMonths;
double monthlyPayment = principle * loanM / (1 - System.Math.Pow((1 + loanM),    negNumberMonths));




//Output the result of the monthly payment
        Console.WriteLine("The amount of the monthly payment is: " + monthlyPayment);
        Console.WriteLine();
        Console.WriteLine("Press the Enter key to end. . .");
        Console.Read();
4

4 に答える 4

11

私がやろうとしているのは、毎月の支払い値を小数点以下 2 桁にフォーマットし、「$」記号を追加することです。

currency format specifierを使用したいようです。

Console.WriteLine("The amount of the monthly payment is: {0:c}", monthlyPayment);

もちろん、常にドル記号を使用するとは限りません。スレッドの現在のカルチャに通貨記号を使用します。いつでもCultureInfo.InvariantCulture明示的に指定できます。

ただし、通貨値には使用しないことを強くお勧めします。代わりにdouble使用してください。decimal

于 2013-04-16T19:06:18.197 に答える
0

Math.Round()次の関数を使用できます。

double inputNumber = 90.0001;
string outputNumber = "$" + Math.Round(inputNumber, 2).ToString();
于 2013-04-16T19:06:37.090 に答える