Java 检索数字的第一位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2967898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 15:00:22  来源:igfitidea点击:

Retrieving the first digit of a number

javatype-conversion

提问by Michoel

I am just learning Java and am trying to get my program to retrieve the first digit of a number - for example 543 should return 5, etc. I thought to convert to a string, but I am not sure how I can convert it back? Thanks for any help.

我只是在学习 Java 并试图让我的程序检索数字的第一个数字 - 例如 543 应该返回 5 等。我想转换为字符串,但我不确定如何将其转换回来?谢谢你的帮助。

int number = 534;
String numberString = Integer.toString(number);
char firstLetterChar = numberString.charAt(0);
int firstDigit = ????

采纳答案by Jordan Allan

    int number = 534;
    int firstDigit = Integer.parseInt(Integer.toString(number).substring(0, 1));

回答by unholysampler

Integer.parseIntwill take a string and return a int.

Integer.parseInt将接受一个字符串并返回一个整数。

回答by Adamski

int firstDigit = Integer.parseInt(Character.toString(firstLetterChar));

回答by Adam

int number = 534;
String numberString = "" + number;
char firstLetterchar = numberString.charAt(0);
int firstDigit = Integer.parseInt("" + firstLetterChar);

回答by Ben Burnett

firstDigit = number/((int)(pow(10,(int)log(number))));

This should get your first digit using math instead of strings.

这应该使用数学而不是字符串来获得您的第一个数字。

In your example log(543) = 2.73 which casted to an int is 2. pow(10, 2) = 100 543/100 = 5.43 but since it's an int it gets truncated to 5

在您的示例中, log(543) = 2.73 转换为 int 是 2. pow(10, 2) = 100 543/100 = 5.43 但由于它是 int 它被截断为 5

回答by Sean

Almost certainly more efficient than using Strings:

几乎肯定比使用字符串更有效:

int firstDigit(int x) {
    while (x > 9) {
        x /= 10;
    }
    return x;
}

(Works only for nonnegative integers.)

(仅适用于非负整数。)

回答by Brian Risk

This example works for any double, not just positive integers and takes into account negative numbers or those less than one. For example, 0.000053 would return 5.

此示例适用于任何双精度数,而不仅仅是正整数,并且考虑了负数或小于 1 的数。例如,0.000053 将返回 5。

private static int getMostSignificantDigit(double value) {
    value = Math.abs(value);
    if (value == 0) return 0;
    while (value < 1) value *= 10;
    char firstChar = String.valueOf(value).charAt(0);
    return Integer.parseInt(firstChar + "");
}

To get the first digit, this sticks with String manipulation as it is far easier to read.

为了获得第一个数字,这坚持使用字符串操作,因为它更容易阅读。

回答by Maysara Alhindi

int number = 534;
int firstDigit = number/100; 

( / ) operator in java divide the numbers without considering the reminder so when we divide 534 by 100 , it gives us (5) .

java 中的 ( / ) 运算符不考虑提醒而对数字进行除法,因此当我们将 534 除以 100 时,它给了我们 (5) 。

but if you want to get the last number , you can use (%) operator

但是如果你想得到最后一个数字,你可以使用 (%) 运算符

    int lastDigit = number%10; 

which gives us the reminder of the division , so 534%10 , will yield the number 4 .

这给了我们除法的提醒,所以 534%10 将产生数字 4 。

回答by Harvey

This way might makes more sense if you don't want to use str methods

如果您不想使用 str 方法,这种方式可能更有意义

int first = 1;
for (int i = 10; i < number; i *= 10) {
    first = number / i;
}