Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scientific Notation in C#

How do I assign a number that is in scientific notation to a variable in C#?

I'm looking to use Plancks Constant which is 6.626 X 10-34

This is the code I have which isn't correct:

 Decimal PlancksConstant = 6.626 * 10e-34;
like image 772
Justin Avatar asked Jan 22 '17 00:01

Justin


People also ask

How do you type a scientific notation?

For scientific notation, you can type either a caret (^) or the letter e followed by a number to indicate an exponent. You can use both positive and negative exponents. For example, to indicate 0.012 , you can enter either of the following expressions: 1.2*10^-2.

What is scientific notation in programming?

Scientific notation is merely a format used for input and output. The 64-bit pattern used for a double inside the computer are the same, no matter what character format was used for input. Here is an example: C:\temp>java DoubleDouble Enter a double: 1.234E+9 value: 1.234E9 twice value: 2.468E9.

How do you write an exponent in C?

Basically in C exponent value is calculated using the pow() function. pow() is function to get the power of a number, but we have to use #include<math. h> in c/c++ to use that pow() function.


2 Answers

You should be able to declare PlancksConstant as a double and multiply 6.626 by 10e-34 like:

double PlancksConstant = 6.626e-34

Demo

like image 51
Timothy G. Avatar answered Sep 19 '22 18:09

Timothy G.


You can set it like this (note the M suffix for the decimal type):

decimal PlancksConstant = 6.626E-34M;

But this will effectively be 0 because you can't represent a number with magnitude less than 1E-28 as a decimal.

So you need to use double instead and can just define this:

double PlancksConstant = 6.626E-34;
like image 45
Matthew Strawbridge Avatar answered Sep 19 '22 18:09

Matthew Strawbridge