什么是“ java”?字符(数字)转换为整数的方式 [英] What's the "java" way of converting chars (digits) to ints

查看:95
本文介绍了什么是“ java”?字符(数字)转换为整数的方式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下代码:

    char x = '5';
    int a0 = x - '0'; // 0
    int a1 = Integer.parseInt(x + ""); // 1
    int a2 = Integer.parseInt(Character.toString(x)); // 2
    int a3 = Character.digit(x, 10); // 3
    int a4 = Character.getNumericValue(x); // 4
    System.out.printf("%d %d %d %d %d", a0, a1, a2, a3, a4);

(第4版记入: casablanca

您认为将字符转换为 最佳方式是什么一个int吗? ( 最佳方式〜= <惯用方式)

What do you consider to be the "best-way" to convert a char into an int ? ("best-way" ~= idiomatic way)

我们不转换char,但表示形式的值。

We are not converting the actual numerical value of the char, but the value of the representation.

例如:

convert('1') -> 1
convert('2') -> 2
....


推荐答案

Character.getNumericValue

这篇关于什么是“ java”?字符(数字)转换为整数的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆