Golang :如何在Golang中计算未来的复利

247 阅读1分钟

这是一个关于如何用go语言计算复利的简短教程。

什么是复利与公式

复利用于计算投资和金融领域中的金额价值。

复利是一种适用于本金的利息,在一段时间内。在数学上,复利有一个公式


Future Compound Interest Amount = principal × ((interest rate/100)+1) power of number
Interest Amount = Future Compound Interest Amount - Amount
  • 本金:本金数额
  • 利率:它是一个百分比值(%)。
  • 数字是一个以年为单位的周期

让我们编写程序来计算Golanguage中的复利 在程序代码中:

  • 用户从控制台输入本金、利息和期限
  • 将所有这些值保存在一个临时变量中
  • 使用上述公式计算未来金额和复利金额
  • 最后打印结果

以下是计算单利的golang程序代码

package main

import (
	"fmt"
)

func main() {
    var principal, interest, period, total compountInterest float64;
    fmt.Print("Please enter principal amount: ")
    fmt.Scanln(&principal)
    fmt.Print("Please enter Interest Rate: ")
    fmt.Scanln(&interest)

    fmt.Print("Please enter period: ")
    fmt.Scanln(&period)

    futureAmount= principal* (math.Pow((1 + interest/100), period))
    compountInterest = futureAmount- principal

    fmt.Println("\nCompound Interest  Amount: ", compountInterest )
    fmt.Println("\n Total  Future Amount: ", futureAmount)


}

输出

Please enter principal amount: 10000
Please enter Interest Rate: 24
Please enter period: 1
nCompound Interest  Amount: 72850
Total  Future Amount: 1.797010299914431e+61

这个程序需要输入--本金、利率和年限,并将这些值保存在一个变量中。

计算出未来和复利,并返回总金额。

总结

在这个例子中,你学会了用Go语言计算复利本金。