当前位置:网站首页>保留两位小数,并向上取值
保留两位小数,并向上取值
2022-07-17 00:13:00 【想做程序媛的小太阳】
遇到一道测评题目,大致意思是结果保留两位小数,并向上取值。在规定时间内认为自己实现了功能,但结果就是不能AC。过后想想大概是因为1.200这种情况,直接变成了1.2,而不是1.20。
首先说保留两位小数向上取值。这里是要解决一个金融上的小问题,ans是计算后的准确结果,后面可能有很多位的小数部分,因为考虑到的是保留两位小数,且是否要向上进0.01只与小数点后第三位有关,于是,采用(int)(ans * 1000)的操作将后面的小数部分去掉,且将结果变成一个整数,这个整数的个位数(ans1 % 10)即为ans的小数点后第三位,在这里判断是否为0,如果不为0,都要将整数的十位数,即原小数的小数点后第二位+1,处理之后,小数点后第三位就可以去掉啦,然后把整数变回小数,由于这里要求返回的是String类型,用toString()方法就可以了。
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;
}
然后说一下后来的思考,对于1.200这种情况,直接变成了1.2,而不是1.20。Java有个DecimalFormat的类,可以很方便的处理数字格式化的问题。(参考来源:点击打开链接)
import java.text.DecimalFormat;
public class Main1{
public static void main(String[]args){
double pi=3.1415927;
//取一位整数
System.out.println(new DecimalFormat("0").format(pi));//3
//取一位整数和两位小数
System.out.println(new DecimalFormat("0.00").format(pi));//3.14
//取两位整数和三位小数,整数不足部分以0填补。
System.out.println(new DecimalFormat("00.000").format(pi));// 03.142
//取所有整数部分
System.out.println(new DecimalFormat("#").format(pi));//3
//以百分比方式计数,并取两位小数
System.out.println(new DecimalFormat("#.##%").format(pi));//314.16%
long c=299792458;//光速
//显示为科学计数法,并取五位小数
System.out.println(new DecimalFormat("#.#####E0").format(c));//2.99792E8
//显示为两位整数的科学计数法,并取四位小数
System.out.println(new DecimalFormat("00.####E0").format(c));//29.9792E7
//每三位以逗号进行分隔。
System.out.println(new DecimalFormat(",###").format(c));//299,792,458
//将格式嵌入文本
System.out.println(new DecimalFormat("光速大小为每秒,###米。").format(c));
}
}
边栏推荐
- 05基于ZigBee的路灯灯控故障检测系统设计
- Pointer constant and constant pointer love and hate
- VIM 配置文件
- ENVI_ Idl: batch re projection of modisswath products (calling the secondary development interface) + parsing
- 搭建map-reduce开发环境
- gdb+vscode进行调试5——gdb查看相关命令
- 搭建Ozzie环境
- Foo bar 什么鬼?
- Envi IDL: lire la teneur en colonne de NO2 de tous les produits OMI et calculer la moyenne mensuelle, la moyenne trimestrielle, la moyenne annuelle + résolution
- DGC最佳实践:机密数据入湖,如何保证数据不被泄露?
猜你喜欢

Fairness in Semi-supervised Learning: Unlabeled Data Help to Reduce Discrimination

静态库与动态库

【白话模电1】PN结与二极管

成信大ENVI_IDL第二周课后作业:提取n个点的气溶胶厚度+详细解析

Recursive and recursive learning notes

ENVI_ Idl: read the NO2 column content of all OMI products and calculate the monthly average, quarterly average, annual average + analysis

Hands on deep learning -- multi layer perceptron (MLP)

ENVI_ Idl: batch re projection of modisswath products (calling the secondary development interface) + parsing

Integrated learning

C语言程序之编译,链接
随机推荐
ENVI_IDL:批量重投影ModisSwath产品(调用二次开发接口)+解析
S32K148EVB 关于ENET Loopback实验
Differences between saber PSPICE Simulink power supply simulation software
One vs One Mitigation of Intersectional Bias
散列表、布隆过滤器、分布式一致性hash
Oozie integrated sh
Determine whether two arrays are exactly equal
Build spark on yarn environment
Allegro Design Entry CIS 和 Orcad Capture CIS 关系
静态库与动态库
成信大ENVI_IDL第一周实验测试:数组的简单运算+详细解析
Hands on deep learning -- multi layer perceptron (MLP)
IGBT 直通短路过程问题分析
Suivi du mode de méthode de l'usine
笔记一之IDL基础内容:常用数据类型_创建数组_类型转换_print输出_基本运算_关系运算
FS32K148调试之WDOG与电源模式
Remote sensing submission process
CAN协议通信
02 design of smart home system based on ZigBee
gdb+vscode进行调试8——使用core分析死循环、死锁、段错误