Write a C program to convert a number in scientific notation to its equivalent decimal form :
Given
8.3e+2 output = 830
2.E-1 output = 0.2
4.3E2 output = 430
Is there any inbuilt library or function in C/C++ which can do this?
or one will have to write his own code, in that case, any easy way to achieve this?
Asked
Active
Viewed 425 times
-2

Eric Postpischil
- 195,579
- 13
- 168
- 312

pensee
- 1
- 1
-
2First decide if you want a solution in C or C++, they are two very different languages. Secondly, what have you tried yourself? Please take some time to read [ask], as well as [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/). Lastly please learn how to [edit] your questions, for example to include a [mcve] of your attempt. – Some programmer dude Nov 26 '20 at 13:44
-
You might like to explore [`scanf()` formatting specs](https://learn.microsoft.com/en-us/cpp/c-runtime-library/scanf-type-field-characters?view=msvc-160) and [`printf()` formatting specs](https://learn.microsoft.com/en-us/cpp/c-runtime-library/format-specification-syntax-printf-and-wprintf-functions?view=msvc-160). – Weather Vane Nov 26 '20 at 13:50
-
The question is what preconditions can be relied upon. E.g. should it be possible to convert 1.23e-100 into decimal form? What is the range of values needed to deal with? – Wör Du Schnaffzig Nov 26 '20 at 13:51
-
@EricPostpischil The problem, now that you've removed the C++ tag, is that the accepted answer posted appears to be invalid. Maybe undo or change your edit? – Adrian Mole Nov 26 '20 at 16:25
-
@AdrianMole: It is up to OP to clarify whether they want C (in which case they should also remove acceptance of the answer) or C++ (in which case the tags should be changed). Vote to close for lack of clarity is fine too. – Eric Postpischil Nov 26 '20 at 16:44
-
@Eric OK. Just thought I'd mention it. – Adrian Mole Nov 26 '20 at 16:46
-
what does OP refer to ? – pensee Nov 26 '20 at 16:56
-
1@pensee: Original Poster. You should state whether you want a C or C++ solution. If you want C, you should remove acceptance of the current answer, as it is incorrect because it is C++. If you want C++, you should add the C++ tag, remove the C tag, and edit the question to refer only to C++, not C. They are different languages, have different rules, and should not mix them in questions except questions that involve interacting between the two or differences between the two. – Eric Postpischil Nov 26 '20 at 18:21
1 Answers
2
try this
#include <iostream>
#include <string>
int32_t main(int32_t argc, char *argv[]) {
std::string str = "8.3e+2";
std::cout << std::stod(str) << std::endl;
return EXIT_SUCCESS;
}

Harry
- 2,177
- 1
- 19
- 33
-
This works with my compiler, but I have a question: why did you write this ```int32_t main, char *argv[]``` instead of just ```int main()```. It seems like Java – justANewb stands with Ukraine Nov 26 '20 at 13:52
-
C takes arguments from the command line, and this is how you catch them, argc is the number of arguments and argv is an array of strings with them. – ACoelho Nov 26 '20 at 13:56
-
@ACoelho There is nothing done with any command line arg. in this mcve. Btw. `int32_t main` instead `int main` is looking somehow strange to me too. (I don't think it's wrong...) – Scheff's Cat Nov 26 '20 at 13:58
-
Thanks This seems to be working. I am a beginner at stackoverflow want to know few things - 1) For new line do we always have to type
?? That was weird I couldnt get a newline without typing this tag. Similarly more than one space directly doesnt work. dont know whats the tag for that. Using this question as an opportunity to learn about stackoverflow syntax. – pensee Nov 26 '20 at 14:00 -
Many text editors auto-complete main to this expression, and `int32_t` can be safer depending on the environment you are running, none of it is necessary for this example though. – ACoelho Nov 26 '20 at 14:00
-
1`int32_t main` is wrong. C++ 2017 (draft n4659) 6.6.1 says `main` shall have a declared return type of `int`. It may be that `int32_t` is an alias of `int` (if `
` is included), but this is not portable. Also, although the OP tagged C and C++ and wrote C/C++ in one place, they asked for a C program, so the balance lies with C, not C++. – Eric Postpischil Nov 26 '20 at 14:21 -
@Harry How is it related to the `C` tag. It will not compile https://godbolt.org/z/8doYqz – 0___________ Nov 26 '20 at 18:39