数字计算工具DecimalComputer
2021/12/30 23:07:04
本文主要是介绍数字计算工具DecimalComputer,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
============================数字计算工具
import java.math.BigDecimal; public class DecimalComputer { /** * 加减乘除运算 * @param a * @param b * @return */ public static BigDecimal add(String a, String b){ BigDecimal aDeci = new BigDecimal(a); BigDecimal bDeci = new BigDecimal(b); return aDeci.add(bDeci); } public static BigDecimal subtract(String a, String b){ BigDecimal aDeci = new BigDecimal(a); BigDecimal bDeci = new BigDecimal(b); return aDeci.subtract(bDeci); } public static BigDecimal multiply(String a, String b){ BigDecimal aDeci = new BigDecimal(a); BigDecimal bDeci = new BigDecimal(b); return aDeci.multiply(bDeci); } public static BigDecimal divide(String a, String b){ BigDecimal aDeci = new BigDecimal(a); BigDecimal bDeci = new BigDecimal(b); return aDeci.divide(bDeci); } /** * 加减乘除运算 * @param a * @param b * @return */ public static BigDecimal add(float a, float b){ BigDecimal aDeci = new BigDecimal(a); BigDecimal bDeci = new BigDecimal(b); return aDeci.add(bDeci); } public static BigDecimal subtract(float a, float b){ BigDecimal aDeci = new BigDecimal(a); BigDecimal bDeci = new BigDecimal(b); return aDeci.subtract(bDeci); } public static BigDecimal multiply(float a, float b){ BigDecimal aDeci = new BigDecimal(a); BigDecimal bDeci = new BigDecimal(b); return aDeci.multiply(bDeci); } public static BigDecimal divide(float a, float b){ BigDecimal aDeci = new BigDecimal(a); BigDecimal bDeci = new BigDecimal(b); return aDeci.divide(bDeci); } /** * 加减乘除运算 * @param a * @param b * @return */ public static BigDecimal add(double a, double b){ BigDecimal aDeci = new BigDecimal(a); BigDecimal bDeci = new BigDecimal(b); return aDeci.add(bDeci); } public static BigDecimal subtract(double a, double b){ BigDecimal aDeci = new BigDecimal(a); BigDecimal bDeci = new BigDecimal(b); return aDeci.subtract(bDeci); } public static BigDecimal multiply(double a, double b){ BigDecimal aDeci = new BigDecimal(a); BigDecimal bDeci = new BigDecimal(b); return aDeci.multiply(bDeci); } public static BigDecimal divide(double a, double b){ BigDecimal aDeci = new BigDecimal(a); BigDecimal bDeci = new BigDecimal(b); return aDeci.divide(bDeci); } /** * 加减乘除运算 * @param aDeci * @param bDeci * @return */ public static BigDecimal add(BigDecimal aDeci, BigDecimal bDeci){ return aDeci.add(bDeci); } public static BigDecimal subtract(BigDecimal aDeci, BigDecimal bDeci){ return aDeci.subtract(bDeci); } public static BigDecimal multiply(BigDecimal aDeci, BigDecimal bDeci){ return aDeci.multiply(bDeci); } public static BigDecimal divide(BigDecimal aDeci, BigDecimal bDeci){ return aDeci.divide(bDeci); } /** * 比较两个数字大小 * @param a * @param b * @return */ public static int compare(String a, String b){ BigDecimal aDeci = new BigDecimal(a); BigDecimal bDeci = new BigDecimal(b); return compare(aDeci, bDeci); } public static int compare(float a, float b){ BigDecimal aDeci = new BigDecimal(a); BigDecimal bDeci = new BigDecimal(b); return compare(aDeci, bDeci); } public static int compare(double a, double b){ BigDecimal aDeci = new BigDecimal(a); BigDecimal bDeci = new BigDecimal(b); return compare(aDeci, bDeci); } public static int compare(BigDecimal aDeci, BigDecimal bDeci){ return aDeci.compareTo(bDeci); } }
============================数字计算工具测试类
/** * 数字运算 */ @Test public void test_computer() { String aStr = "6.40"; String bStr = "8.00"; System.out.println(DecimalComputer.add(aStr, bStr)); double aDouble = Double.valueOf(aStr); double bDouble = Double.valueOf(bStr); System.out.println(DecimalComputer.add(aDouble, bDouble)); float aFloat = Float.valueOf(aStr); float bFloat = Float.valueOf(bStr); System.out.println(DecimalComputer.add(aFloat, bFloat)); BigDecimal aDeci = new BigDecimal(aStr); BigDecimal bDeci = new BigDecimal(bStr); System.out.println(DecimalComputer.add(aDeci, bDeci)); System.out.println(DecimalComputer.subtract(aStr, bStr)); System.out.println(DecimalComputer.subtract(aDouble, bDouble)); System.out.println(DecimalComputer.subtract(aFloat, bFloat)); System.out.println(DecimalComputer.subtract(aDeci, bDeci)); System.out.println(DecimalComputer.multiply(aStr, bStr)); System.out.println(DecimalComputer.multiply(aDouble, bDouble)); System.out.println(DecimalComputer.multiply(aFloat, bFloat)); System.out.println(DecimalComputer.multiply(aDeci, bDeci)); System.out.println(DecimalComputer.divide(aStr, bStr)); System.out.println(DecimalComputer.divide(aDouble, bDouble)); System.out.println(DecimalComputer.divide(aFloat, bFloat)); System.out.println(DecimalComputer.divide(aDeci, bDeci)); }
这篇关于数字计算工具DecimalComputer的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2025-01-03用LangChain构建会检索和搜索的智能聊天机器人指南
- 2025-01-03图像文字理解,OCR、大模型还是多模态模型?PalliGema2在QLoRA技术上的微调与应用
- 2025-01-03混合搜索:用LanceDB实现语义和关键词结合的搜索技术(应用于实际项目)
- 2025-01-03停止思考数据管道,开始构建数据平台:介绍Analytics Engineering Framework
- 2025-01-03如果 Azure-Samples/aks-store-demo 使用了 Score 会怎样?
- 2025-01-03Apache Flink概述:实时数据处理的利器
- 2025-01-01使用 SVN合并操作时,怎么解决冲突的情况?-icode9专业技术文章分享
- 2025-01-01告别Anaconda?试试这些替代品吧
- 2024-12-31自学记录鸿蒙API 13:实现人脸比对Core Vision Face Comparator
- 2024-12-31自学记录鸿蒙 API 13:骨骼点检测应用Core Vision Skeleton Detection