ftoa



Автор Дмитрий Жук задал вопрос в разделе Другие языки и технологии

ftoa c++ и получил лучший ответ

Ответ от Ѕулиганов Иосиф[гуру]
ftoa - функция чистого С, а не С++. Наверное, нужно включить stdio.h. Мне, например, всегда больше нравилась функция sprintf(). Она тоже из stdio.h/

Ответ от Димос Тцарев[гуру]
#include <sstream>
std::ostringstream strs;
strs << dbl;
std::string str = strs.str();

Ответ от AlexPride[гуру]
// C program for implementation of ftoa()
#include<stdio.h>
#include<math.h>
// reverses a string 'str' of length 'len'
void reverse(char *str, int len)
{
int i=0, j=len-1, temp;
while (i<j)
{
temp = str[i];
str[i] = str[j];
str[j] = temp;
i++; j--;
}
}
// Converts a given integer x to string str[]. d is the number
// of digits required in output. If d is more than the number
// of digits in x, then 0s are added at the beginning.
int intToStr(int x, char str[], int d)
{
int i = 0;
while (x)
{
str[i++] = (x%10) + '0';
x = x/10;
}
// If number of digits required is more, then
// add 0s at the beginning
while (i < d)
str[i++] = '0';
reverse(str, i);
str[i] = '';
return i;
}
// Converts a floating point number to string.
void ftoa(float n, char *res, int afterpoint)
{
// Extract integer part
int ipart = (int)n;
// Extract floating part
float fpart = n - (float)ipart;
// convert integer part to string
int i = intToStr(ipart, res, 0);
// check for display option after point
if (afterpoint != 0)
{
res[i] = '.'; // add dot
// Get the value of fraction part upto given no.
// of points after dot. The third parameter is needed
// to handle cases like 233.007
fpart = fpart * pow(10, afterpoint);
intToStr((int)fpart, res + i + 1, afterpoint);
}
}
// driver program to test above funtion
int main()
{
char res[20];
float n = 233.007;
ftoa(n, res, 4);
printf("
"%s"
", res);
return 0;
}

Ответ от Duality[гуру]
В с++11 есть замечательный std::to_string же.

Ответ от Лиля Калиде[гуру]
Пользуй sprintf().

Ответ от 3 ответа[гуру]
Привет! Вот подборка тем с похожими вопросами и ответами на Ваш вопрос: ftoa c++
Itoa Си на Википедии
Посмотрите статью на википедии про Itoa Си
 

Ответить на вопрос:

Имя*

E-mail:*

Текст ответа:*
Проверочный код(введите 22):*