当前位置:网站首页>Keep two decimal places and take values upward
Keep two decimal places and take values upward
2022-07-19 02:42:00 【Little sun who wants to be a program yuan】
Encounter an evaluation question , It roughly means that the result retains two decimal places , And take value upward . I think I have realized the function within the specified time , But the result is that you can't AC. Think about it later, probably because 1.200 This situation , It becomes 1.2, instead of 1.20.
First of all, keep two decimal places and take values upward . Here is to solve a small financial problem ,ans It is the accurate result after calculation , There may be many decimal places after it , Because it's about keeping two decimal places , And whether to make progress 0.01 It is only related to the third digit after the decimal point , therefore , use (int)(ans * 1000) The operation of will remove the following decimal part , And turn the result into an integer , The single digits of this integer (ans1 % 10) That is to say ans The third digit after the decimal point of , Judge whether it is 0, If not for 0, The ten digits of an integer , That is, the second place after the decimal point of the original decimal +1, After processing , The third digit after the decimal point can be removed , Then change the integer back to decimal , Because what is required here is String type , use toString() The method is ok .
static String calculate(int a, double i, int n) {
double ans = a * (i /1200) * Math.pow((1+i /1200),(n * 12)) / (Math.pow((1+i /1200),(n * 12))-1);
int ans1 = (int)(ans*1000);
if(ans1 % 10 != 0)
ans1 = ans1 + 10;
ans1 = ans1 - ans1 % 10;
double res = ans1/1000.0;
String result = Double.toString(res);
return result;
}
Then talk about later thinking , about 1.200 This situation , It becomes 1.2, instead of 1.20.Java There is one DecimalFormat Class , It is very convenient to deal with the problem of number formatting .( Reference source : Click to open the link )
import java.text.DecimalFormat;
public class Main1{
public static void main(String[]args){
double pi=3.1415927;
// Take an integer
System.out.println(new DecimalFormat("0").format(pi));//3
// Take one integer and two decimal places
System.out.println(new DecimalFormat("0.00").format(pi));//3.14
// Take two integers and three decimal places , The insufficient part of an integer is to 0 fill .
System.out.println(new DecimalFormat("00.000").format(pi));// 03.142
// Take all integral parts
System.out.println(new DecimalFormat("#").format(pi));//3
// Count as a percentage , And take two decimal places
System.out.println(new DecimalFormat("#.##%").format(pi));//314.16%
long c=299792458;// The speed of light
// It's shown as scientific counting , And take five decimal places
System.out.println(new DecimalFormat("#.#####E0").format(c));//2.99792E8
// Scientific counting that is displayed as two digit integers , And take four decimal places
System.out.println(new DecimalFormat("00.####E0").format(c));//29.9792E7
// Every three digits are separated by commas .
System.out.println(new DecimalFormat(",###").format(c));//299,792,458
// Embedding formatting into text
System.out.println(new DecimalFormat(" The speed of light is per second ,### rice .").format(c));
}
}
边栏推荐
- [hsjframework] unity time management timemanger timer
- How to use nmon
- SSH远程控制与访问
- Use JMeter to test services based on websocket protocol
- BeanShell脚本获取当前时间
- [unity development tips] unity mixer mixer controls global volume
- Swagger -- the most popular API framework in the world
- 已知先序遍历中序遍历,求树的层序遍历
- 种下一颗种子,十年后长成了参天B+树
- 时间管理方法的反思与探讨
猜你喜欢

Post man JSON script to JMX script of JMeter

shell脚本接收和返回参数
![[solved] after referring to the local MySQL and forgetting the password, [server] --initialize specified but the data directory has files in it Aborti](/img/a8/2daa2c0d834f1986c8421bf5138c7e.png)
[solved] after referring to the local MySQL and forgetting the password, [server] --initialize specified but the data directory has files in it Aborti

Shell编程规范与变量

Traversal of binary tree

Firewalld 防火墙

WINRAR命令拷贝指定文件夹为压缩文件,调用计划任务进行备份。

Use of sqlmap

Bladex - a well-designed microservice architecture

使用Grafana8.5.2显示zabbix6.0的信息
随机推荐
通过Xshell7使用rz,sz命令上传下载文件
接口(Collection/Map)- 各接口的实现与对比
How to use nmon
Shell脚本for、while循环语句、猜价格小游戏
How to configure multiple SSH keys for novices (easy to understand hand-in-hand teaching)
[unity Editor Extension] find all objects of a script attached in the scene and resources
Zabbix6.0通过iDRAC,IMM2监控DELL,IBM服务器硬件
全链路压测
Shell编程规范与变量
SSH远程控制与访问
innodb、Mysql结构、三种删除的区别
shell脚本之条件语句
[antv G2] how to solve the memory leak caused by G2
已知先序遍历中序遍历,求树的层序遍历
shell脚本之循环语句与函数
[unity Editor Extension] displays the memory size of all files in the resource directory
[Ruiji takeout ⑩] rough learning of Linux & rough learning of redis
性能瓶颈定位XMind
网络一般知识(详)
登录功能的测试点大全