怎么样将字符串的第一个(数字)字符转换为Java中的整数?

huangapple go评论61阅读模式
英文:

How do you turn the first (numerical) character of a string into an integer in Java?

问题

以下是翻译好的部分:

假设我有一个值为1.5的双精度浮点数,然后我将其转换为字符串,然后尝试使用char.at方法将第一个字符转换为字符串。

double a = 1.5;
String b = String.valueOf(a);
System.out.println(Integer.valueOf(b.charAt(0)));

但是当我这样做时,我得到的输出是:

49
我做错了什么?
英文:

So let's say I have a double which is 1.5, and then I convert it to a string, then try to turn the first character into a string using the char.at method.

double a = 1.5;
String b = String.valueOf(a);
System.out.println(Integer.valueOf(b.charAt(0)));

But when I did that, here's the output I received:

49

What did I do wrong?

答案1

得分: 7

valueOf接受一个int或者一个String。你传递了一个char(由charAt返回),它被提升为一个整数。你可以使用substring来获取字符串的第一个字符。

double a = 1.5;
String b = String.valueOf(a);
System.out.println(Integer.valueOf(b.substring(0, 1)));
英文:

valueOf takes either an int or a String. You passed a char (returned by charAt) which got promoted to an integer. You can use substring to get the first character of the string instead.

double a = 1.5;
String b = String.valueOf(a);
System.out.println(Integer.valueOf(b.substring(0, 1)));

答案2

得分: 3

It's taking the "ascii" value of the first character.

If you want to pass Integer.valueOf a String as a parameter, try the following:

double a = 1.5;
String b = String.valueOf(a);
System.out.println(Integer.valueOf(Character.toString(b.charAt(0))));

Also, if you want to pass an integer, Integer.valueOf can also be used as following:

double a = 1.5;
String b = String.valueOf(a);
System.out.println(Integer.valueOf(Character.getNumericValue(b.charAt(0))));

Output:

1
英文:

It's taking the ascii value of the first character.

If you want to pass Integer.valueOf a String as a parameter, try the following:

double a = 1.5;
String b = String.valueOf(a);
System.out.println(Integer.valueOf(Character.toString(b.charAt(0))));

Also, if you want to pass an integer, Integer.valueOf can also be used as following:

double a = 1.5;
String b = String.valueOf(a);
System.out.println(Integer.valueOf(Character.getNumericValue(b.charAt(0))));

Output:

1

答案3

得分: 3

只需使用Character.getNumericValue(b.charAt(0)),以获取实际值,而不是ASCII。

英文:

Just use Character.getNumericValue(b.charAt(0));
to get actual value instead of ASCII

答案4

得分: 2

有多种方法可以做到这一点,其中之一如下(使用 Double 类):

Double number = 1.5;
System.out.println(number.intValue());

intValue() 方法将返回 Double 实例的整数表示。

英文:

There are various methods to do this, one is the following (using the Double class):

Double number = 1.5;
System.out.println(number.intValue());

The intValue() method will return the int representation of the istance of Double

答案5

得分: 1

parseInt也能够工作

    double a = 1.5;
    String b = String.valueOf(a);
    int c = Integer.parseInt(b.substring(0, 1));
    System.out.println(c);
英文:

parseInt will work too:

double a = 1.5;
String b = String.valueOf(a);
int c = Integer.parseInt(b.substring(0,1));
System.out.println(c);

答案6

得分: 1

以下是翻译好的内容:

ASCII值为`'1'`的字符是`49`,这就是你得到的结果。为了获得`1`,你需要从中减去`'0'`或者`48`(`'0'`的ASCII值)。

public class Main {
    public static void main(String[] args) {
        double a = 1.5;
        String b = String.valueOf(a);
        System.out.println(b.charAt(0) - '0');
    }
}
输出:

1
另外,你也可以使用 [`Character#getNumericValue`][1] 来获取字符的数值。

public class Main {
    public static void main(String[] args) {
        double a = 1.5;
        String b = String.valueOf(a);
        System.out.println(Character.getNumericValue(b.charAt(0)));
    }
}

输出:

1

[1]: https://docs.oracle.com/javase/7/docs/api/java/lang/Character.html#getNumericValue(int)
英文:

The ASCII value of '1' is 49 which is what you are getting. In order to get 1, you need to subtract '0' or 48 (the ASCII value of '0') from it.

public class Main {
	public static void main(String[] args) {
		double a = 1.5;
		String b = String.valueOf(a);
		System.out.println(b.charAt(0) - '0');
	}
}

Output:

1

Alternatively, you can use Character#getNumericValue to get the numeric value of the character.

public class Main {
	public static void main(String[] args) {
		double a = 1.5;
		String b = String.valueOf(a);
		System.out.println(Character.getNumericValue(b.charAt(0)));
	}
}

Output:

1

huangapple
  • 本文由 发表于 2020年10月18日 17:16:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64411602.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定