+ 6
Manav Roy
I said split the value with dot (.) separator or use regular expression to extract value after decimal.
Example given by her is not programmatical because you don't know which value you have to subtract.
And also after decimal there maybe x number of digits so also you can't identify how many digits after decimal.
So in programming learn about "how to split anything" and "how to use regex".
For this you have to see many examples on Google for different programming languages.
+ 5
If it's pure math logic that you want
1. Value subtract the whole number before the decimal
2. Assuming x is the number of decimal places given, multiply number from step 1 by 10^x
So in your example 5.65
1. 5.65 - 5 = 0.65
2. x=2
0.65 * 10^2 = 65
So that ends with 65
+ 1
Manav Roy
Just split with decimal or you can use regex.
+ 1
Manav Roy
If you're looking for code in c++
#include <iostream>
using namespace std;
int main()
{
double input = 282.6481;
string itr = to_string(input), result="";
bool val = 0;
for (int i = 0; i < int(itr.size()); i++){
if (itr[i] == '.'){
val = 1;
continue; }
if (val){
result +=itr[i]; }
}
for (;;){
if (result[result.size()-1] == '0'){
result=result.substr(0,result.size()-1); }
else{ break ; }
}
int final = stoi(result);
cout << final;
return 0;
}
0
Manav Roy
What happened
0
I don't remember if I studied this before, it was a very long time ago, but I do remember teaching this to students
0
Runtime Terror yeah my answer is meant for finite decimals places, wouldn't work if you try to apply that to an non-terminating decimal infinity and want exact answers. You will need to determine if it is a rational number and if so turn the numbers after the decimal into a fraction to show the exact result (like 1/3) otherwise it's pretty much impossible (like PI). It still work though if you only care about precision up to a finite number of digits (like for PI up to 2 decimal places would be 14)
Plus I have not written my answer considering any programming efficiency. There is probably a easier way to do this in the bit shifting computer languages
0
floatNum - intNum
or
floatNum % intNum
you can convert it to string, get the index of the dot, slice it.
0
I think he is given a string with a number in it and he has to use split funcion o regex.
Dunno what language u using so i cant tell u how to use split function, prolly split(".") returning an array or maybe you have to call it several times, one per match.
Regex could be: /\d+\.\d+/
Depending what language you using u will have to retrieve matches from calling regex function. Usually return an array.You need the second match (first is the int part).
0
Hey i am new helllo
0
And the result has to be a float?
float split(float in) {
in = fabs(in); //absolute value
in = in - floor(in);
return in;
}
the returning value would be 0.xxxx where x is the floating part
If u only want to print those numbers u could do
float t, n=split(inputnumber);
// i is max number of digits u want to print
// in case value has to many digits, i set it to 10
for (int i=10; i>0,n>0; i--) {
n*=10;
t=floor(n);
printf("%d", (int)t);
n=n-t;
}
printf("\n");
or u could convert float to string then use regex to split string
or in raw c:
char fstr[256];
char *decimal;
snprintf(fstr,256,"%f",inputnumber);//convert to str
decimal=strchr(fstr, '.');
if(decimal) decimal++; //next char after .
//decimal would be a string with decimal numbers
// or a null string
0
Manav Roy hahaha your question came a hot topic
0
Yaxye Hassan Hello! Welcome!