Java 将字符转换为双精度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19987628/
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-12 22:05:19  来源:igfitidea点击:

Conversion Char to double

javachardouble

提问by JFC

How may I convert a numerical value in the form of a char to a double value?

如何将字符形式的数值转换为双精度值?

I've tried just casting the char to a double but... it doesn't work like that I'm guessing as char such as '4' will convert to 52.0 in doubles.

我试过只是将字符转换为双精度,但是...它不会像我猜测那样工作,因为像 '4' 这样的字符会在双精度中转换为 52.0。

So is there a way to convert a char with a value of say char c = '4' to a double value of 4.0 where I can actually perform mathematical calculations on the value?

那么有没有办法将值为 char c = '4' 的 char 转换为 4.0 的双精度值,我可以在其中实际对值执行数学计算?

This is just a little program I created to show that casting a numeric char directly to a double won't work the way I was expecting.

这只是我创建的一个小程序,用于表明将数字字符直接转换为双精度不会像我预期的那样工作。

public class conversion
{
public static void main(String args[])
{
    char eight = '8';
    char four = '4';

    double d2 = (char)eight;
    double d1 = (char)four;

    System.out.println(d2);
    System.out.println(d1);

    double result = (d2 / d1);

    System.out.println(result);
}
}

outputs:

输出:

56.0
52.0
1.0769230769230769

采纳答案by higuaro

You can do:

你可以做:

double d2 = (double) Character.digit(eight, 10);
double d1 = (double) Character.digit(four, 10);

Or:

或者:

double d2 = (double) (eight - '0');
double d1 = (double) (four - '0');

If you want to convert a whole string, use Double.parseDouble

如果要转换整个字符串,请使用 Double.parseDouble

double d2 = Double.parseDouble("15.5");

Beware of a possible NumberFormatExceptionis the string is an invalid floating point number

当心一个可能NumberFormatException的字符串是一个无效的浮点数

回答by arshajii

You can subtract '0'then cast as a double:

您可以减去'0'然后将其转换为 a double

char c = '8';

double d = (double) (c - '0');  // <--

System.out.println(d);
8.0

You can check that cis valid via '0' <= c && c <= '9'.

您可以c通过'0' <= c && c <= '9'.

回答by Alexis C.

You can also use :

您还可以使用:

double d2 = Double.valueOf(String.valueOf(eight));
double d1 = Double.valueOf(String.valueOf(four));

回答by Mike Strobel

The character '0'does not correspond to the integer 0. It corresponds to the ASCII key code for the '0'character. Fortunately, the key codes for the characters '0'through '9'are sequential, so you can simply subtract '0'to convert a numeric character to the integer number it represents:

字符'0'不对应于整数0。它对应于字符的 ASCII 键码'0'。幸运的是,字符'0'通过的键码'9'是连续的,因此您可以简单'0'地将数字字符进行减法转换为它所代表的整数:

double d2 = (char)(eight - '0');
double d1 = (char)(four - '0');

回答by yts

How about this?

这个怎么样?

char doubleChar = '8';
double doubleDouble = Double.parseDouble(Character.toString(doubleChar));

回答by jdev

When you cast like that it returns the ASCII code of 4 or 8. The following code is what you want:

当你这样转换时,它会返回 4 或 8 的 ASCII 代码。以下代码是你想要的:

char myc='4';
double myd= Double.parseDouble(Character.toString(myc));

回答by Velarde

You need to convert the char to a string and then to double like this:

您需要将字符转换为字符串,然后像这样加倍:

double d2 = Double.parseDouble(Character.toString(eight));
double d1 = Double.parseDouble(Character.toString(four));