Converting Strings to Floating Point Values

From Nutwiki
Jump to: navigation, search

Test Environments

Hardware Comments Nut/OS
4.8.7
Ethernut 1.3 H OK
Binaries
Compiler: AVR-GCC 4.3.2
Ethernut 2.1 B OK
Binaries
Compiler: AVR-GCC 4.3.2
Ethernut 3.0 E NO

Overview

This example demonstrates a few methods for converting strings containing numbers to floating point numerical values. This example builds on the Converting Strings to Numerical Values example.

Before you begin, please enable floating point support for your project. In order to do this, open the configurator, load the appropriate config file for your project and then activate the following option:

File:Floatactivation.png

Now rebuild Nut/OS to apply the changed configuration.

Source Code

<source lang="c">

  1. include <dev/board.h>
  2. include <stdio.h>
  3. include <io.h>

int main(void) {

   unsigned long baud = 115200;
   char input[256];
   float number = 0;
   NutRegisterDevice(&DEV_UART, 0, 0);
   freopen(DEV_UART_NAME, "w", stdout);
   freopen(DEV_UART_NAME, "r", stdin);
   _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
   puts("\nNut/OS String to Float Value Conversion Demo");
   puts("Please enter a number: (whitespaces and trailing characters allowed)");
   scanf("%s", input);
   printf("\nConverted to float your number looks like this: %f\n\n", atof(input));
   sprintf(input, "hello 123.456 world");
   printf("A string contains the following value: %s\n We are going to extract the number from it.\n", input);
   sscanf(input, "%*s %f %*s", &number);
   printf("The number was: %f", number);
   for (;;);
   return 0;

} </source>

Details

<source lang="c"> unsigned long baud = 115200; char input[256]; int number = 0;

NutRegisterDevice(&DEV_UART, 0, 0); freopen(DEV_UART_NAME, "w", stdout); freopen(DEV_UART_NAME, "r", stdin); _ioctl(_fileno(stdout), UART_SETSPEED, &baud);

puts("\nNut/OS String to Float Value Conversion Demo"); </source>

Since we are going to ask for some input for the example, we need the UART driver, not the debug one.

<source lang="c"> puts("Please enter a number: (whitespaces and trailing characters allowed)"); scanf("%s", input);

printf("\nConverted to float your number looks like this: %f\n\n", atof(input)); </source>

First we let the user enter a string. Strings can contain lots of different things but in this example we will try to obtain a numerical value from within it.

The main difference to the previous example is that we now use the atof function. This function is able to do what the atoi and atol functions do but it is able to correctly understand and convert the decimal dot in a numerical expression. Just like atoi and atol it will first remove any whitespaces in front of the number and then attempt to convert the number into something that fits into a float or the highest possible value for floats (which may differ depending on the architecture). If there are any characters after the number, they are ignored.

<source lang="c"> sprintf(input, "hello 123.456 world"); printf("A string contains the following value: %s\n We are going to extract the number from it.\n", input);

sscanf(input, "%*s %f %*s", &number);

printf("The number was: %f", number); </source>

Besides that, there is another useful function: sscanf It allows us to break down a string into pieces as we need it. We'll use this handy function to obtain a number from the middle of a string. The first parameter specifies the string to operate on. The second parameter is a format string similar to printf or scanf, the following parameters are the variables we want to store our data in.

Unlike in the previous example we're not using "%d" here but "%f". Besides that, nothing changes. For this to work we also need the "*" format specifier, which does read the data accordingly but not store it anywhere. If you wanted to you could store any number of variables with this function.

Output

<source lang="text"> Nut/OS String to Float Value Conversion Demo Please enter a number: (whitespaces and trailing characters allowed)

 12.34

Converted to float your number looks like this: 12.340000

A string contains the following value: hello 123.456 world

We are going to extract the number from it.

The number was: 123.455990 </source>

See also