标签: number-manipulation

返回数字的第n位数

public class Return {    
    public static void main(String[] args) {        
        int answer = digit(9635, 1);
        print("The answer is " + answer);
    }

    static void print(String karen) {
        System.out.println (karen);
    }

    static int digit(int a, int b) {
        int digit = a;
        return digit;
    }     
}
Run Code Online (Sandbox Code Playgroud)

创建一个使用名为digit的函数的程序,该函数返回整数参数右侧第n位的值.n的值应该是第二个参数.

例如:digit(9635, 1)返回5digit(9635, 3)返回6.

java number-manipulation

2
推荐指数
1
解决办法
2万
查看次数

在基地10舍入

我有一个函数将给定函数的数字舍入到最近的整数便士.

<script type='text/javascript'>
Math.roundNumber = function(a,b){
    if(!isNaN(parseInt(a))){
        c = Math.pow(10,b);
        return (Math.round(a*c)/c);
    }
    return false;
}
</script>
Run Code Online (Sandbox Code Playgroud)

但是我注意到输入到函数中的所述数字必须四舍五入到最接近的一个便士,但它必须四舍五入到小数点后两位.

例如

15.236562213541684 would = 15.24  
9846.65456169846 would = 9846.66

我认为这只是一个改变回报的例子(Math.round(a c)/ c); 返回(Math.ceil(a c)/ c);

显然我错了.

对此事有何帮助?

**编辑**

这是我试图实现的公式,也许它会有所帮助

a = intrest price
b = terms
c = a/b
d = c*(b-1)
e = a-d
Run Code Online (Sandbox Code Playgroud)

所以例如

a = 295.30
b = 156
c = 295.30/156 = 1.90 (rounded up to nearest decimal as above)
d = 1.90 * (b-1) = 294.50
e …
Run Code Online (Sandbox Code Playgroud)

javascript math number-manipulation

1
推荐指数
1
解决办法
873
查看次数

根据总位数反转一个数字

javascript中是否有内置函数可以根据总数反转一个数字?如果没有如何实施呢?

number = 2;
total_number = 10;
__________________
answer = 9;
Run Code Online (Sandbox Code Playgroud)
reverse(2, 10) // returns 9
Run Code Online (Sandbox Code Playgroud)

以来:

 1  == 10
[2  ==  9]
 3  ==  8
 4  ==  7
 5  ==  6
 6  ==  5
 7  ==  4
 8  ==  3
 9  ==  2
 10 ==  1
Run Code Online (Sandbox Code Playgroud)

javascript number-manipulation

0
推荐指数
1
解决办法
382
查看次数

标签 统计

number-manipulation ×3

javascript ×2

java ×1

math ×1