当前位置:网站首页>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));
}
}
边栏推荐
- How to add software shortcuts to the right mouse button list
- Understand HTTP cache in 30 minutes
- For solopi app performance test
- [antv G2] how to solve the memory leak caused by G2
- Next array - circular section
- 子网划分(详)
- postman的json脚本转jmeter的jmx脚本
- Use JMeter to test services based on websocket protocol
- Cocoon breaking and rebirth of 3D NFT: caduceus decentralized edge rendering technology
- Unity notes 1
猜你喜欢

逆元(名字太多人用我就加这几个字)

Inverse yuan (I'll add these words if there are too many people using the name)

Shell脚本整数值比较、逻辑测试、if语句、提取性能监控指标

After unity imports the FBX model, the rotation and position of the object will change automatically at runtime

解决WIN10连接共享打印机出现0x00000709的错误

CTFHub----RCE

Sword finger offer 48 The longest substring without repeated characters

Use JMeter to test services based on websocket protocol

ctfhub--ssrf

Reprint: SQL injection common bypass
随机推荐
Array Transformer-分块思想
数组、冒泡的认识
Interface (collection/map) - implementation and comparison of interfaces
网络一般知识(详)
Use of sqlmap
CTFHub----RCE
The solution to the bounce and offset of unity3d game characters when jumping to the ground
Subnet division (see details)
登录功能的测试点大全
SSH远程控制与访问
网络层协议和IP数据包的格式(详解)
Shortest circuit / secondary short circuit /k short circuit
Leetcode --- one question per day
[unity development tips] unity mixer mixer controls global volume
WINRAR命令拷贝指定文件夹为压缩文件,调用计划任务进行备份。
LAMP平台部署及应用
Understanding: what is interface and the concept of interface
【瑞吉外卖⑩】Linux 粗略学习 & Redis 粗略学习
不会叭不会叭,昨天真有人没写出二进制枚举
逆元(名字太多人用我就加这几个字)