Java 如何将十进制值作为新的整数,并从左向右遍历该整数?
我有一个商业案例,需要将十进制值作为一个新的整数和 然后从左向右遍历它以进行计算Java 如何将十进制值作为新的整数,并从左向右遍历该整数?,java,Java,我有一个商业案例,需要将十进制值作为一个新的整数和 然后从左向右遍历它以进行计算 Eg: I have a integer value int val=1345679; square root of val is double sqrt_val=1160.03405122; //sqrt(1345679), decimal values are limit to 8 digits Now I need decimal value(03405122) to store it int
Eg: I have a integer value int val=1345679;
square root of val is double sqrt_val=1160.03405122; //sqrt(1345679), decimal values are limit to 8 digits
Now I need decimal value(03405122) to store it into integer variable
int decimalValue=03405122;
With this decimal value I want to verify with some number which is given by business.
Let's take some number as 45.
now I have to verify decimalValue until it meets the below condition
03405122<=45 if yes just take the decimal value
if no then remove the 1st digit from left side until condition satisfiet
3405122<=45
405122<=45
05122<=45
5122<=45
122<=45
22<=45.
So, 22 is the number I have to take for further implementation.
//和某个数比较,找出随机数
public int findRandomNumber(int value, int totalRange) {
int _val;
System.out.println("calling rec::: val:" + value);
if (value <= totalRange) {
System.out.println("Success... returing on final value:" + value);
return value;
}
String new_str = String.valueOf(value);
String final_str = new_str.substring(1);
// System.out.println("str:"+final_str);
int val = Integer.parseInt(final_str);
// System.out.println("val:"+val);
_val = findRandomNumber(val, totalRange);
return _val;
// System.out.println("Returning flag:"+flag);
}
public int findRandomNumber(int-value,int-totalRange){
国际价值;
System.out.println(“调用记录:::val:+value”);
如果(value您提供了一个纯伪代码的问题,我将慷慨地提供纯伪代码的答案:
1) 获取sqrt\u val
2) 使用该方法仅获取
3) 循环通过所述子字符串的长度2
4) 如果字符串的值小于目标值,请检查每次迭代
5a)如果是,那就是你的结果
5b)如果不是,则从位置1开始获取子字符串
1如果以0开头,您的结果可能会有所不同,因此我将留给您解决
2如果使用这种方法,请小心循环通过String.length
,因为在中使用子字符串
,否则
我不相信这是最好的解决方案,但这里有一个不使用字符串的替代解决方案
import java.math.BigDecimal;
public class Test {
public static void main(String[] args) {
// Value to be less than
BigDecimal lessThan = new BigDecimal(80);
int test = 333444;
BigDecimal bd = new BigDecimal( Math.sqrt(test) );
System.out.println(bd);
while( bd.scale() > 0 ) {
// get the fractional value and make it a whole number
BigDecimal fractionOnly = bd.divideAndRemainder(BigDecimal.ONE)[1];
fractionOnly = fractionOnly.movePointRight(fractionOnly.scale());
// do the check
System.out.println( fractionOnly + " < " + lessThan );
if ( fractionOnly.compareTo( lessThan ) < 0 ) {
System.out.println( "Yes" );
break;
}
// method kinda says it
bd = bd.movePointRight(1);
}
}
}
import java.math.BigDecimal;
公开课考试{
公共静态void main(字符串[]args){
//值小于
BigDecimal lessThan=新的BigDecimal(80);
内部测试=333444;
BigDecimal bd=新的BigDecimal(Math.sqrt(test));
系统输出打印项次(bd);
而(bd.scale()>0){
//获取分数并使其成为一个整数
BigDecimal fractionOnly=bd.divideandMainder(BigDecimal.ONE)[1];
fractionOnly=fractionOnly.movePointRight(fractionOnly.scale());
//检查
System.out.println(仅分数+“<”+小于分数);
如果(仅分形。比较(小于)小于0){
System.out.println(“是”);
打破
}
//方法就是这样
bd=bd.movePointRight(1);
}
}
}
你可以将数字转换成字符串,然后用一个不断缩小的字符串对这个字符串和你的数字进行数字比较,直到你的“嗨,这个论坛是专门为编程而设的。你可能对算法有疑问,但你至少需要指定一种实现算法的语言。也许你可以发布d你的问题在错误的论坛上,或者你需要指定你打算用哪种语言实现你的algo。我不明白这与Spring或Java EE有什么关系。这是一个简单的Java编程任务。对不起,伙计们,这并不是说我没有做任何作业,也不是要求做作业。我的目的只是想看看不同的想法和需要选择最好的。我正在用我的代码更新帖子。如果我能用更好的方式写,请帮助我。谢谢!非常好。你可能想添加编译该文章所需的类和导入语句,使其完整。我不想让他们太容易:)不过,这一点很好,我将添加进去。
import java.math.BigDecimal;
public class Test {
public static void main(String[] args) {
// Value to be less than
BigDecimal lessThan = new BigDecimal(80);
int test = 333444;
BigDecimal bd = new BigDecimal( Math.sqrt(test) );
System.out.println(bd);
while( bd.scale() > 0 ) {
// get the fractional value and make it a whole number
BigDecimal fractionOnly = bd.divideAndRemainder(BigDecimal.ONE)[1];
fractionOnly = fractionOnly.movePointRight(fractionOnly.scale());
// do the check
System.out.println( fractionOnly + " < " + lessThan );
if ( fractionOnly.compareTo( lessThan ) < 0 ) {
System.out.println( "Yes" );
break;
}
// method kinda says it
bd = bd.movePointRight(1);
}
}
}