java中j<num i .length_判断数值有效位数的Java工具类详解

public class AdjustmentDouble{

/**

* @param s 需处理的数据

* @param numOfIntPart 整数位数

* @param numOfDecimalPart 小数位数

* @return 处理过的数据

*/

public static String adjustDouble(String s,int numOfIntPart,int numOfDecimalPart){

//按小数点的位置分割成整数部分和小数部分

String[] array = s.split("\\.");

char[] tempA=new char[numOfIntPart];

char[] tempB=new char[numOfDecimalPart];

//整数部分满足精度要求(情况1)

if(array[0].length()==numOfIntPart){

//直接获取整数部分长度字符

for(int i=0;i

tempA[i]=array[0].charAt(i);

}

//小数部分精度大于或等于指定的精度

if(numOfDecimalPart<=array[1].length()){

for(int i=0;i

tempB[i]=array[1].charAt(i);

}

}

//小数部分精度小于指定的精度

if(numOfDecimalPart>array[1].length()){

for(int i=0;i

if(i

tempB[i]=array[1].charAt(i);

}else{

tempB[i]='0';

}

}

}

if(numOfDecimalPart==0){

return String.valueOf(tempA)+String.valueOf(tempB);

}

return String.valueOf(tempA)+"."+String.valueOf(tempB);

}

//整数部分位数大于精度要求(情况2)

if(array[0].length()>numOfIntPart){

//先倒序获取指定位数的整数

for(int i=array[0].length()-1,j=0;(i>=array[0].length()-numOfIntPart)&&(j

tempA[j]=array[0].charAt(i);

System.out.println(tempA[j]);

}

char[] tempA1=new char[numOfIntPart];

//调整顺序

for(int j=0,k=tempA.length-1;j=0);j++,k--){

tempA1[j]=tempA[k];

System.out.println("tempA1[j]"+tempA1[j]);

}

//小数部分精度大于或等于指定的精度

if(numOfDecimalPart<=array[1].length()){

for(int i=0;i

tempB[i]=array[1].charAt(i);

}

}

//小数部分精度小于指定的精度

if(numOfDecimalPart>array[1].length()){

for(int i=0;i

if(i

tempB[i]=array[1].charAt(i);

}else{

tempB[i]='0';

}

}

}

return String.valueOf(tempA1)+"."+String.valueOf(tempB);

}

//整数部分满足精度要求(情况3)

if(array[0].length()==numOfIntPart){

//直接获取整数部分长度字符

for(int i=0;i

tempA[i]=array[0].charAt(i);

}

//小数部分精度小于指定的精度

if(numOfDecimalPart>array[1].length()){

for(int i=0;i

if(i

tempB[i]=array[1].charAt(i);

}else{

tempB[i]='0';

}

}

}

//小数部分精度大于或等于指定的精度

if(numOfDecimalPart<=array[1].length()){

for(int i=0;i

tempB[i]=array[1].charAt(i);

}

}

if(numOfDecimalPart==0){

return String.valueOf(tempA)+String.valueOf(tempB);

}

return String.valueOf(tempA)+"."+String.valueOf(tempB);

}

//整数部分大于精度要求(情况4)

if(array[0].length()>numOfIntPart){

//先倒序获取指定位数的整数

for(int i=array[0].length()-1,j=0;(i>=array[0].length()-numOfIntPart+1)&&(j

//System.out.println("<<<

tempA[j]=array[0].charAt(i);

}

char[] tempA1=new char[numOfIntPart];

//调整顺序

for(int j=0,k=tempA.length-1;j=0);j++){

tempA1[j]=tempA[k];

k--;

}

//小数部分精度小于指定的精度

if(numOfDecimalPart>array[1].length()){

for(int i=0;i

if(i>=array[1].length()){

tempB[i]='0';

}else{

tempB[i]=array[1].charAt(i);

}

}

}

//小数部分精度大于或等于指定的精度

if(numOfDecimalPart<=array[1].length()){

for(int i=0;i

tempB[i]=array[1].charAt(i);

}

}

if(numOfDecimalPart==0){

return String.valueOf(tempA1)+String.valueOf(tempB);

}

return String.valueOf(tempA1)+"."+String.valueOf(tempB);

}

//整数部分小于精度要求(情况5)

if(array[0].length()

//先倒序获取指定位数的整数

char[] tempA1=new char[numOfIntPart];

for(int i=array[0].length()-1,j=0;(i>=numOfIntPart-array[0].length()-(numOfIntPart-array[0].length()))&&(j

tempA1[j]=array[0].charAt(i);

System.out.println("<<<<<

}

//补0

for(int i=array[0].length();i

tempA1[i]='0';

System.out.println("<<<<<

}

char[] tempA2=new char[numOfIntPart];

//调整顺序

for(int j=0,k=tempA1.length-1;j=0);j++){

tempA2[j]=tempA1[k];

k--;

}

//小数部分精度小于指定的精度

if(numOfDecimalPart>array[1].length()){

for(int i=0;i

if(i

tempB[i]=array[1].charAt(i);

}else{

tempB[i]='0';

}

}

}

//小数部分精度大于或等于指定的精度

if(numOfDecimalPart<=array[1].length()){

for(int i=0;i

tempB[i]=array[1].charAt(i);

}

}

if(numOfDecimalPart==0){

return String.valueOf(tempA2)+String.valueOf(tempB);

}

return String.valueOf(tempA2)+"."+String.valueOf(tempB);

}

//情况(6)

if((array[0].length()

for(int i=0; i< numOfIntPart-array[0].length(); i++){

s = "0"+s ;

}

for(int i = 0 ; i < numOfDecimalPart -array[1].length() ; i++){

s = s+"0" ;

}

return s;

}

return null;

}

/**

* @param args

*/

public static void main(String[] args) {

// TODO Auto-generated method stub

String s = "1234.123";

s = AdjustmentDouble.adjustDouble(s, 5, 6);

System.out.println(s);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/** * @project: WebProjectUtil * @class: NumberUtil * @describe: 此工具类用来处理数字方面的逻辑, * 如返回指定位数的随机数字、Double的加减乘除精确运算、指定位数数字用“0”补齐 * @autho: Administrator * @date: 2013-6-7 下午02:26:27 * @alter: Administrator * @alterDate: 2013-6-7 下午02:26:27 * @alterRemark: * @version V1.0 */ public class NumberUtil { private static final int DEF_DIV_SCALE = 2; /** * @return 返回12位随机数 */ public static String randomNumber() { } /** * @param parm * @return 返回指定位数随机数 */ public static String randomNumber(int parm) { } /** * * 两个Double数相加 * * @param v1 * @param v2 * @return Double */ public static Double add(Double v1, Double v2) { } /** * * 两个Double数相减 * * @param v1 * @param v2 * @return Double */ public static Double sub(Double v1, Double v2) { } /** * * 两个Double数相乘 * * @param v1 * @param v2 * @return Double */ public static Double mul(Double v1, Double v2) { } /** * * 两个Double数相除 * * @param v1 * @param v2 * @return Double */ public static Double div(Double v1, Double v2) { } /** * * 两个Double数相除,并保留scale位小数 * * @param v1 * @param v2 * @param scale * @return Double */ public static Double div(Double v1, Double v2, int scale) { } /** * 返回指定Double的负数 * @param v1 * @return */ public static Double neg(Double v1) { /** * @Title: toFixdLengthString * @Description: 将字符串用符号填充位数 * @param str 源字符串 * @param fixdlenth 位数 * @return String * @throws */ public static String toFixdLengthString(String str, int fixdlenth) { } /** * @Title: toFixdLengthString * @Description: 将数字用“0”填充位数 * @param num * @param fixdlenth * @return String * @throws */ public static String toFixdLengthString(int num, int fixdlenth) { } /** * @Title: generateSpaceString * @Description: 得到指定位数占位符 * @param length * @return String * @throws */ public static String generateSpaceString(int length) { } /** * @Title: generateZeroString * @Description: 得到指定位数的“0”的占位符 * @param length * @return String * @throws */ public static String generateZeroString(int length) { } }
#include <iostream> #include <cstring> using namespace std; #define MAX_LENGTH 100 // 字符串逆序 void reverse(string& s) { int left = 0, right = s.length() - 1; while (left < right) { swap(s[left], s[right]); left++, right--; } } // 字符串加法 string add(string num1, string num2) { // 字符串逆序 reverse(num1), reverse(num2); // 用 0 补齐,使它们的长度相等 if (num1.length() < num2.length()) { num1 += string(num2.length() - num1.length(), '0'); } else { num2 += string(num1.length() - num2.length(), '0'); } // 从低位开始,将两个字符串对应位相加,考虑进位,将结果存到一个新的字符串 string result; int carry = 0; for (int i = 0; i < num1.length(); i++) { int s = num1[i] - '0' + num2[i] - '0' + carry; carry = s / 10; result += to_string(s % 10); } // 如果最高位有进位,则将进位也加上 if (carry > 0) { result += to_string(carry); } // 字符串逆序,删除前导 0,输出结果 reverse(result); result.erase(0, result.find_first_not_of('0')); if (result.empty()) { result = "0"; } return result; } // 字符串减法 string subtract(string num1, string num2) { // 比较被减数和减数的大小 if (num1.length() < num2.length() || (num1.length() == num2.length() && num1 < num2)) { swap(num1, num2); num1 = '-' + num1; } // 字符串逆序 reverse(num1), reverse(num2); // 用 0 补齐,使它们的长度相等 num2 += string(num1.length() - num2.length(), '0'); // 从低位开始,将两个字符串对应位相减,考虑借位,将结果存到一个新的字符串 string result; int borrow = 0; for (int i = 0; i < num1.length(); i++) { int s = num1[i] - '0' - num2[i] + borrow; if (s < 0) { s += 10; borrow = -1; } else { borrow = 0; } result += to_string(s); } // 字符串逆序,删除前导 0,输出结果 reverse(result); result.erase(0, result.find_first_not_of('0')); if (result.empty()) { result = "0"; } return result; } int main() { string num1, num2, op; cin >> num1 >> op >> num2; if (op == "+") { cout << add(num1, num2) << endl; } else { cout << subtract(num1, num2) << endl; } return 0; }
最新发布
06-08
这段代码存在两个问题: 1. 代码的第一行和第二行之间有一个空格,这会导致编译器无法正确解析头文件。应该将这两行代码合并为一行,去掉间的空格,即: #include <iostream> #include <cstring> 2. 字符串减法存在一个逻辑错误。在对两个字符串进行减法时,如果被减数小于减数,应该将它们交换位置,并在被减数前加上负号。但是,在这段代码,交换位置的语句和添加负号的语句是写在两个不同的 if 分支的,这会导致在被减数和减数相等且都为 0 的情况下出现错误。应该将这两个语句合并为一句: if (num1.length() < num2.length() || (num1.length() == num2.length() && num1 < num2)) { swap(num1, num2); num1 = '-' + num1; } 修改后的代码如下: ``` #include <iostream> #include <cstring> using namespace std; #define MAX_LENGTH 100 void reverse(string& s) { int left = 0, right = s.length() - 1; while (left < right) { swap(s[left], s[right]); left++, right--; } } string add(string num1, string num2) { reverse(num1), reverse(num2); if (num1.length() < num2.length()) { num1 += string(num2.length() - num1.length(), '0'); } else { num2 += string(num1.length() - num2.length(), '0'); } string result; int carry = 0; for (int i = 0; i < num1.length(); i++) { int s = num1[i] - '0' + num2[i] - '0' + carry; carry = s / 10; result += to_string(s % 10); } if (carry > 0) { result += to_string(carry); } reverse(result); result.erase(0, result.find_first_not_of('0')); if (result.empty()) { result = "0"; } return result; } string subtract(string num1, string num2) { if (num1.length() < num2.length() || (num1.length() == num2.length() && num1 < num2)) { swap(num1, num2); num1 = '-' + num1; } reverse(num1), reverse(num2); num2 += string(num1.length() - num2.length(), '0'); string result; int borrow = 0; for (int i = 0; i < num1.length(); i++) { int s = num1[i] - '0' - num2[i] + borrow; if (s < 0) { s += 10; borrow = -1; } else { borrow = 0; } result += to_string(s); } reverse(result); result.erase(0, result.find_first_not_of('0')); if (result.empty()) { result = "0"; } return result; } int main() { string num1, num2, op; cin >> num1 >> op >> num2; if (op == "+") { cout << add(num1, num2) << endl; } else { cout << subtract(num1, num2) << endl; } return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值