我正在尝试使用System.out.printf将非常大的数字舍入到2位小数。这是我正在使用的代码:
System.out.printf(" %1.2f = overpayment:$" + overpayment);
我收到此错误:
Exception in thread "main" java.util.MissingFormatArgumentException: Format specifier '%1.2f'
at java.util.Formatter.format(Unknown Source)
at java.io.PrintStream.format(Unknown Source)
我的印象是%f是格式说明符,%1.2f用于小数点后2位数的浮点数。 我试图将4.4260494195128784E-4舍入到4.43。谢谢
由于我多付了一分钱,我认为我有错误的公式来计算它。有没有人知道如何获得多笔贷款?多付价值应为4.43。这是我的代码:
import java.util.Scanner;
公共类CreditCardPayoff {
public static void main(String[] args) {
// TODO Auto-generated method stub
double principle;
double annualInterestRate;
double monthlyPayment;
double numerator;
double denominator;
double monthsToPayOff;
double monthsToPayOffCeiled;
double totalAmountPaid;
double totalInterestPaid;
double overpayment;
Scanner keyboard = new Scanner(System.in);
principle = keyboard.nextDouble();
annualInterestRate = keyboard.nextDouble();
monthlyPayment = keyboard.nextDouble();
numerator = Math.log(monthlyPayment) - Math.log(monthlyPayment-
(annualInterestRate / 1200.00) * principle);
denominator = Math.log((annualInterestRate/1200.00) + 1.0);
monthsToPayOff = numerator/denominator;
monthsToPayOffCeiled = Math.ceil(monthsToPayOff);
totalAmountPaid = monthsToPayOffCeiled * monthlyPayment;
totalInterestPaid = totalAmountPaid - principle;
overpayment = (monthsToPayOffCeiled - monthsToPayOff)/monthlyPayment;
System.out.println("Principle:" + principle);
System.out.println("Annual Interest Rate:" + annualInterestRate);
System.out.println("Monthly Payment:" + monthlyPayment);
System.out.println("Months Needed To Pay Off:" + (int)monthsToPayOffCeiled);
System.out.println("Total Amount Paid: $" + totalAmountPaid);
System.out.println("Total Interest Paid: $" + totalInterestPaid);
System.out.printf("overpayment: $%1.2f ", overpayment);
}
}
我计算多付款的方法,获得monthsToPayOff和monthsToPayOffCeil(上限)之间的差额,然后除以每月付款。这让我得到了4.4260494195128784E-4。
答案 0 :(得分:0)
这不是您使用$RunAs32Bit = {
[string]$providerName ="Microsoft Base Smart Card Crypto Provider"
# import CrytoAPI from advapi32.dll
$signature = @"
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
[return : MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptGetProvParam(
IntPtr hProv,
uint dwParam,
byte[] pbProvData,
ref uint pdwProvDataLen,
uint dwFlags);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
[return : MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptDestroyKey(
IntPtr hKey);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
[return : MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptAcquireContext(
ref IntPtr hProv,
string pszContainer,
string pszProvider,
uint dwProvType,
long dwFlags);
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
[return : MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptGetUserKey(
IntPtr hProv,
uint dwKeySpec,
ref IntPtr phUserKey);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptGetKeyParam(
IntPtr hKey,
uint dwParam,
byte[] pbData,
ref uint pdwDataLen,
uint dwFlags);
[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
[return : MarshalAs(UnmanagedType.Bool)]
public static extern bool CryptReleaseContext(
IntPtr hProv,
uint dwFlags);
"@
$CryptoAPI = Add-Type -member $signature -name advapiUtils -Namespace CryptoAPI -passthru
# set some constants for CryptoAPI
$AT_KEYEXCHANGE = 1
$AT_SIGNATURE = 2
$PROV_RSA_FULL = 1
$KP_CERTIFICATE = 26
$PP_ENUMCONTAINERS = 2
$PP_CONTAINER = 6
$PP_USER_CERTSTORE = 42
$CRYPT_FIRST = 1
$CRYPT_NEXT = 2
$CRYPT_VERIFYCONTEXT = 0xF0000000
[System.IntPtr]$hProvParent=0
$contextRet = $CryptoAPI::CryptAcquireContext([ref]$hprovParent,$null,$providerName,$PROV_RSA_FULL,$CRYPT_VERIFYCONTEXT)
[Uint32]$pdwProvDataLen = 0
[byte[]]$pbProvData = $null
$GetProvParamRet = $CryptoAPI::CryptGetProvParam($hprovParent,$PP_CONTAINER,$pbProvData,[ref]$pdwProvDataLen,0)
if($pdwProvDataLen -gt 0)
{
$ProvData = new-Object byte[] $pdwProvDataLen
$GetKeyParamRet = $CryptoAPI::CryptGetProvParam($hprovParent,$PP_CONTAINER,$ProvData,[ref]$pdwProvDataLen,0)
}
$enc = new-object System.Text.UTF8Encoding($null)
$keyContainer = $enc.GetString($ProvData)
write-host " The Default User Key Container:" $keyContainer
[Uint32]$pdwProvDataLen = 0
[byte[]]$pbProvData = $null
$GetProvParamRet = $CryptoAPI::CryptGetProvParam($hprovParent,$PP_USER_CERTSTORE,$pbProvData,[ref]$pdwProvDataLen,0)
if($pdwProvDataLen -gt 0)
{
$ProvData = new-Object byte[] $pdwProvDataLen
$GetKeyParamRet = $CryptoAPI::CryptGetProvParam($hprovParent,$PP_USER_CERTSTORE,$ProvData,[ref]$pdwProvDataLen,0)
[uint32]$provdataInt = [System.BitConverter]::ToUInt32($provdata,0)
[System.IntPtr]$hwStore = $provdataInt
}
$store = new-object System.Security.Cryptography.X509Certificates.X509Store($hwStore)
# release smart card
$ReleaseContextRet = $CryptoAPI::CryptReleaseContext($hprovParent,0)
return $store
}
#Run the code in 32bit mode if PowerShell isn't already running in 32bit mode
If($env:PROCESSOR_ARCHITECTURE -ne "x86"){
Write-Warning "Non-32bit architecture detected, collecting certificate information in separate 32bit process."
$Job = Start-Job $RunAs32Bit -RunAs32
$SCStore = $Job | Wait-Job | Receive-Job
}Else{
$SCStore = $RunAs32Bit.Invoke()
}
的方式。
printf
了解这一点,您还可以添加额外的格式:
System.out.printf("%1.2f and %d", 1.25f, 1000);
在格式字符串中,您可以指定数据类型,然后将每个数据类型作为参数传递。
答案 1 :(得分:0)
为了使用String.format
,您不会将占位符的值与格式字符串连接起来,而是将它们作为参数传递:
Drawer
答案 2 :(得分:0)
printf
为每个格式说明符提供一个参数(请参阅documentation)。在Java中,参数由逗号分隔(,
)。
因此,在您的示例中,您应该使用逗号来代替连接两个字符串的+
(字符串和字符串中的数字):
System.out.printf(" %1.2f = overpayment:$", overpayment);