Golang :如何在Golang中计算单利本金

199 阅读1分钟

这是一个关于如何使用golang计算单利息的简短教程

什么是单利与公式

单利是一种适用于本金的利息,在一段时期内以利率计算。

通常情况下,为了用数学术语计算单利,我们使用以下公式:

    Simple Interest=P x N x R/100
  • P是本金数额
  • N是月数
  • R是利率

在一个程序代码中:

  • 输入本金数额、利息和期限
  • 将所有这些存储在一个变量中
  • 使用上述公式进行计算
  • 最后打印结果

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

package main

import (
	"fmt"
)

func main() {
    var principal, interest, period, total 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)

    total= (principal* interest* period) / 100

    fmt.Println("\nSimple Interest  Amount: ", total)
    fmt.Println("\n Total  Amount: ", amount+total)


}

输出

Please enter principal amount: 10000
Please enter Interest Rate: 24
Please enter period: 1
Simple Interest  Amount: 2400
Total  Amount: 12400

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

计算单利并返回总金额。

总结

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