将字符串转换为浮点数

[英]Converting a Character String to a Float


I'm trying to take the user input, which is a character string, and convert it to a float. In my case, gas keeps being printed as 55.000000 when the user enters 7 - I'd like it to be printed as 7.0.

我正在尝试获取用户输入,这是一个字符串,并将其转换为浮点数。在我的情况下,当用户输入7时,气体保持打印为55.000000 - 我希望它打印为7.0。

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <math.h>


int main()
{
    char gas_gallons;
    float gas;
    printf("Please enter the number of gallons of gasoline: ");
    scanf("%c", &gas_gallons);

    while (!isdigit(gas_gallons))
    {
        printf("\nYou need to enter a digit. Please enter the number of gallons of gasoline: ");
        scanf("%c", &gas_gallons);
        }
    if (isdigit(gas_gallons))
    {
        printf("\nHello %c", gas_gallons);
        gas = gas_gallons;
        printf("\nHello f", gas);
    }
    return 0;
}

4 个解决方案

#1


1  

Why not do this? It's much simpler.

为什么不这样做?它简单得多。

#include<stdio.h>
int main()
{
    int gas;
    printf("Please enter the number of gallons of gasoline.\n : ");
    //use the %d specifier to get an integer.  This is more direct.
    //It will also allow the user to order more than 9 gallons of gas.
    scanf("%d", &gas);
    printf("\nHello %d", gas);//prints integer value of gas
    //Using the .1f allows  you to get one place beyond the decimal
    //so you get the .0 after the integer entered.
    printf("\nHello %.1f\n", (float) gas);//print floating point 
    return 0;
}

#2


1  

You said:

你说:

In my case, gas keeps being printed as 55.000000 when the user enters 7

在我的情况下,当用户输入7时,气体保持打印为55.000000

When the user enters 7 as input the digit 7 is stored as the character in gas_gallons. The decimal value of the character 7 is 55 in ASCII encoding. You can see the decimal values of other characters in ASCII encoding at Wikipedia and many other places on the web.

当用户输入7作为输入时,数字7被存储为gas_gallons中的字符。 ASCII编码中字符7的十进制值为55。您可以在Wikipedia和Web上的许多其他位置查看ASCII编码中其他字符的十进制值。

When you use:

当你使用:

 gas = gas_gallons;

the integer value of gas_gallons is, i.e. 55, is assigned to gas. That explains why you get 55.000000 as the output when you print gas.

gas_gallons的整数值,即55,被分配给气体。这就解释了为什么在打印气体时输出为55.000000。

You can fix the problem many ways. Here are a couple of suggestions.

您可以通过多种方式解决问题。以下是一些建议。

Option 1

选项1

Convert the digit to a number by using:

使用以下方法将数字转换为数字:

 gas = gas_gallons - '0';

Option 2

选项2

Discard the code to read the number of gallons of gasoline as a digit and converting the digit to a number. Using a digit is also limiting since you cannot have 10 or 12.5 as input.

丢弃代码以读取汽油加仑数作为数字并将数字转换为数字。使用数字也是有限制的,因为您不能将10或12.5作为输入。

Read the number of gallons of gasoline as a number directly. With this approach, your input can be a any floating point number that can be represented by a float.

直接读取加仑汽油的数量。使用此方法,您的输入可以是任何浮点数,可以由浮点数表示。

#include <stdio.h>

int main()
{
   float num_gallons;

   while ( 1 )
   {
      printf("Please enter the number of gallons of gasoline: ");

      // Read the number of gallons of gas.
      // If reading is successful, break of the loop.
      if (scanf("%f", &num_gallons) == 1 )
      {
         break;
      }

      // There was an error.
      // Read and discard the rest of the line in the input
      // stream.
      scanf("%*[^\n]%*c");

      printf("There was an error in reading the gallons of gasoline.\n");
   }

   printf("\nHello %f\n", num_gallons);
   return 0;
}

#3


0  

The ASCII-characters '0'-'9' do not have the integer values 0-9. You can find the appropriate value by subtracting '0'.

ASCII字符'0' - '9'没有整数值0-9。您可以通过减去“0”找到适当的值。

#4


0  

To convert a string to float you can use atof (ASCII to float) function included in stdlib.h. Here is the full declaration of this function: double atof(const char *str) so you can do a simple cast gas = (float) atof(gas_gallons);

要将字符串转换为float,可以使用stdlib.h中包含的atof(ASCII到float)函数。这是这个函数的完整声明:double atof(const char * str)所以你可以做一个简单的cast gas =(float)atof(gas_gallons);

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.silva-art.net/blog/2015/05/26/39f383be7140706ea0e48e661f5db893.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告