Last active
December 27, 2019 22:09
-
-
Save QApolo/43802fb9968a01d0f48ead2b869e8370 to your computer and use it in GitHub Desktop.
Hope it helps
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using namespace std; | |
void insertIntIntoCharArr(int num, char arr[], int &pos) | |
{ | |
while(num > 0) | |
{ | |
arr[pos++] = num % 10 + 48; | |
num /= 10; | |
} | |
} | |
void floatToString(float num, char arr[], int afterPoint) | |
{ | |
int numInt = (int)num; | |
int pot = 1; | |
while(afterPoint--) pot *= 10; | |
int numFloatPart = (int)(pot * ((float(numInt) ) - num) *-1.0 ); | |
int index = 0; | |
insertIntIntoCharArr(numFloatPart, arr, index); | |
arr[index] = '.'; index++; | |
insertIntIntoCharArr(numInt, arr, index); | |
//backwards | |
int l = 0, r = index-1; | |
char aux; | |
while(l < r) | |
{ | |
aux = arr[l]; | |
arr[l] = arr[r]; | |
arr[r] = aux; | |
l++; r--; | |
} | |
} | |
int main() | |
{ | |
char arr[10]; | |
floatToString(123.524, arr, 3); | |
cout << arr << endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment