• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

wchenfeng

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

金融运用:计算未来投资回报

 编写程序,读取投资总额、年利率和年数,然后使用下面的公式显示投资回报金额。

 

未来投资回报金额 = 投资金额 * Math.pow((1 + 月利率), 年数*12)

 

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class zuoye2_21 {

	public static void main(String[] args)throws IOException{
		// TODO 自动生成的方法存根
		BufferedReader buf;
	    buf=new BufferedReader(new InputStreamReader(System.in));
	    double a,b,score;int c;
		System.out.print("Enter investment amount:");
		a=Double.parseDouble(buf.readLine());
		System.out.print("Enter annual interset rate in percentage:");
		b=Double.parseDouble(buf.readLine());
		System.out.print("Enter number of years:");
		c=Integer.parseInt(buf.readLine());
		score=(1+0.01*b/12);
		score=Math.pow(score, c*12);
		score=a*score;
		System.out.print("Future value is $"+score);
	}

}

 

输出

 

输入

1000.56
4.25
1

最后的输出语句进行更改

System.out.print("Future value is $");
System.out.println(String.format("%.2f", score));

输出 

 

 

Java中控制输出小数位数 的三种写法

import java.text.DecimalFormat;
import java.text.NumberFormat; 

第一种
DecimalFormat df = new DecimalFormat("#0.00");
float data;
data = sc.nextFloat();
System.out.println(df.format(data));

第二种

模仿c语言的输出方式
System.out.printf("%.2f\n", data);

第三种
System.out.println(String.format("%.2f", data));

值得注意是,java和c/c++不同的是double对应的数据类型是 %f,而非%lf;

posted on 2022-04-12 20:02  王陈锋  阅读(108)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3