Scientific Notation formatting in Python










0















How can get following formatting (input values are always less than 0.1):



> formatting(0.09112346)
0.91123E-01
> formatting(0.00112346)
0.11234E-02


and so on.



I am looking for some elegant solution. I am already using a custom function given below:



def formatting(x, prec=5):
tup = x.as_tuple()
digits = list(tup.digits[:prec])
dec = ''.join(str(i) for i in digits)
exp = x.adjusted()
return '0.decEexp'.format(dec=dec, exp=exp)









share|improve this question
























  • You mean how to get a specific notation when printing a number?

    – toti08
    Nov 13 '18 at 6:52











  • i think yes. (i am not very clear on the concept of "specific notation")

    – ARK4579
    Nov 13 '18 at 6:57











  • Yeah, sorry, I mean printing the number with different notation, like exponential notation, or specifying only a certain amount of digits after the comma, and so on...

    – toti08
    Nov 13 '18 at 7:00















0















How can get following formatting (input values are always less than 0.1):



> formatting(0.09112346)
0.91123E-01
> formatting(0.00112346)
0.11234E-02


and so on.



I am looking for some elegant solution. I am already using a custom function given below:



def formatting(x, prec=5):
tup = x.as_tuple()
digits = list(tup.digits[:prec])
dec = ''.join(str(i) for i in digits)
exp = x.adjusted()
return '0.decEexp'.format(dec=dec, exp=exp)









share|improve this question
























  • You mean how to get a specific notation when printing a number?

    – toti08
    Nov 13 '18 at 6:52











  • i think yes. (i am not very clear on the concept of "specific notation")

    – ARK4579
    Nov 13 '18 at 6:57











  • Yeah, sorry, I mean printing the number with different notation, like exponential notation, or specifying only a certain amount of digits after the comma, and so on...

    – toti08
    Nov 13 '18 at 7:00













0












0








0








How can get following formatting (input values are always less than 0.1):



> formatting(0.09112346)
0.91123E-01
> formatting(0.00112346)
0.11234E-02


and so on.



I am looking for some elegant solution. I am already using a custom function given below:



def formatting(x, prec=5):
tup = x.as_tuple()
digits = list(tup.digits[:prec])
dec = ''.join(str(i) for i in digits)
exp = x.adjusted()
return '0.decEexp'.format(dec=dec, exp=exp)









share|improve this question
















How can get following formatting (input values are always less than 0.1):



> formatting(0.09112346)
0.91123E-01
> formatting(0.00112346)
0.11234E-02


and so on.



I am looking for some elegant solution. I am already using a custom function given below:



def formatting(x, prec=5):
tup = x.as_tuple()
digits = list(tup.digits[:prec])
dec = ''.join(str(i) for i in digits)
exp = x.adjusted()
return '0.decEexp'.format(dec=dec, exp=exp)






python formatting scientific-notation






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 7:55







ARK4579

















asked Nov 13 '18 at 6:09









ARK4579ARK4579

10318




10318












  • You mean how to get a specific notation when printing a number?

    – toti08
    Nov 13 '18 at 6:52











  • i think yes. (i am not very clear on the concept of "specific notation")

    – ARK4579
    Nov 13 '18 at 6:57











  • Yeah, sorry, I mean printing the number with different notation, like exponential notation, or specifying only a certain amount of digits after the comma, and so on...

    – toti08
    Nov 13 '18 at 7:00

















  • You mean how to get a specific notation when printing a number?

    – toti08
    Nov 13 '18 at 6:52











  • i think yes. (i am not very clear on the concept of "specific notation")

    – ARK4579
    Nov 13 '18 at 6:57











  • Yeah, sorry, I mean printing the number with different notation, like exponential notation, or specifying only a certain amount of digits after the comma, and so on...

    – toti08
    Nov 13 '18 at 7:00
















You mean how to get a specific notation when printing a number?

– toti08
Nov 13 '18 at 6:52





You mean how to get a specific notation when printing a number?

– toti08
Nov 13 '18 at 6:52













i think yes. (i am not very clear on the concept of "specific notation")

– ARK4579
Nov 13 '18 at 6:57





i think yes. (i am not very clear on the concept of "specific notation")

– ARK4579
Nov 13 '18 at 6:57













Yeah, sorry, I mean printing the number with different notation, like exponential notation, or specifying only a certain amount of digits after the comma, and so on...

– toti08
Nov 13 '18 at 7:00





Yeah, sorry, I mean printing the number with different notation, like exponential notation, or specifying only a certain amount of digits after the comma, and so on...

– toti08
Nov 13 '18 at 7:00












1 Answer
1






active

oldest

votes


















0














You can use the format() function. The format specification mentions it there:




'E' - Exponent notation. Same as 'e' except it uses an upper case ‘E’ as the separator character.




>>> print(':.5E'.format(0.09112346))
9.11235E-02
>>> print(':.5E'.format(0.00112346))
1.12346E-03



However it isn't quite like the output you have in your answer. If the above is not satisfactory, then you might use a custom function to help (I'm not the best at this, so hopefully it's ok):



def to_scientific_notation(number):
a, b = ':.4E'.format(number).split('E')
return ':.5fE:+03d'.format(float(a)/10, int(b)+1)

print(to_scientific_notation(0.09112346))
# 0.91123E-01
print(to_scientific_notation(0.00112346))
# 0.11234E-02





share|improve this answer

























  • yes, sorry but, that's not the output I am looking for. I am already using a custom function (in my answer)

    – ARK4579
    Nov 13 '18 at 7:37






  • 1





    @ARK4579 Um, sorry, did you read the whole answer? The 2nd part gives the format you asked for (0.91123E-01 and 0.11234E-02) or am I missing something?

    – Jerry
    Nov 13 '18 at 7:39











  • yes, it does. thanks. :) but i am already using a custom function. :)

    – ARK4579
    Nov 13 '18 at 7:53










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274821%2fscientific-notation-formatting-in-python%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You can use the format() function. The format specification mentions it there:




'E' - Exponent notation. Same as 'e' except it uses an upper case ‘E’ as the separator character.




>>> print(':.5E'.format(0.09112346))
9.11235E-02
>>> print(':.5E'.format(0.00112346))
1.12346E-03



However it isn't quite like the output you have in your answer. If the above is not satisfactory, then you might use a custom function to help (I'm not the best at this, so hopefully it's ok):



def to_scientific_notation(number):
a, b = ':.4E'.format(number).split('E')
return ':.5fE:+03d'.format(float(a)/10, int(b)+1)

print(to_scientific_notation(0.09112346))
# 0.91123E-01
print(to_scientific_notation(0.00112346))
# 0.11234E-02





share|improve this answer

























  • yes, sorry but, that's not the output I am looking for. I am already using a custom function (in my answer)

    – ARK4579
    Nov 13 '18 at 7:37






  • 1





    @ARK4579 Um, sorry, did you read the whole answer? The 2nd part gives the format you asked for (0.91123E-01 and 0.11234E-02) or am I missing something?

    – Jerry
    Nov 13 '18 at 7:39











  • yes, it does. thanks. :) but i am already using a custom function. :)

    – ARK4579
    Nov 13 '18 at 7:53















0














You can use the format() function. The format specification mentions it there:




'E' - Exponent notation. Same as 'e' except it uses an upper case ‘E’ as the separator character.




>>> print(':.5E'.format(0.09112346))
9.11235E-02
>>> print(':.5E'.format(0.00112346))
1.12346E-03



However it isn't quite like the output you have in your answer. If the above is not satisfactory, then you might use a custom function to help (I'm not the best at this, so hopefully it's ok):



def to_scientific_notation(number):
a, b = ':.4E'.format(number).split('E')
return ':.5fE:+03d'.format(float(a)/10, int(b)+1)

print(to_scientific_notation(0.09112346))
# 0.91123E-01
print(to_scientific_notation(0.00112346))
# 0.11234E-02





share|improve this answer

























  • yes, sorry but, that's not the output I am looking for. I am already using a custom function (in my answer)

    – ARK4579
    Nov 13 '18 at 7:37






  • 1





    @ARK4579 Um, sorry, did you read the whole answer? The 2nd part gives the format you asked for (0.91123E-01 and 0.11234E-02) or am I missing something?

    – Jerry
    Nov 13 '18 at 7:39











  • yes, it does. thanks. :) but i am already using a custom function. :)

    – ARK4579
    Nov 13 '18 at 7:53













0












0








0







You can use the format() function. The format specification mentions it there:




'E' - Exponent notation. Same as 'e' except it uses an upper case ‘E’ as the separator character.




>>> print(':.5E'.format(0.09112346))
9.11235E-02
>>> print(':.5E'.format(0.00112346))
1.12346E-03



However it isn't quite like the output you have in your answer. If the above is not satisfactory, then you might use a custom function to help (I'm not the best at this, so hopefully it's ok):



def to_scientific_notation(number):
a, b = ':.4E'.format(number).split('E')
return ':.5fE:+03d'.format(float(a)/10, int(b)+1)

print(to_scientific_notation(0.09112346))
# 0.91123E-01
print(to_scientific_notation(0.00112346))
# 0.11234E-02





share|improve this answer















You can use the format() function. The format specification mentions it there:




'E' - Exponent notation. Same as 'e' except it uses an upper case ‘E’ as the separator character.




>>> print(':.5E'.format(0.09112346))
9.11235E-02
>>> print(':.5E'.format(0.00112346))
1.12346E-03



However it isn't quite like the output you have in your answer. If the above is not satisfactory, then you might use a custom function to help (I'm not the best at this, so hopefully it's ok):



def to_scientific_notation(number):
a, b = ':.4E'.format(number).split('E')
return ':.5fE:+03d'.format(float(a)/10, int(b)+1)

print(to_scientific_notation(0.09112346))
# 0.91123E-01
print(to_scientific_notation(0.00112346))
# 0.11234E-02






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 13 '18 at 8:01

























answered Nov 13 '18 at 7:29









JerryJerry

58k1068102




58k1068102












  • yes, sorry but, that's not the output I am looking for. I am already using a custom function (in my answer)

    – ARK4579
    Nov 13 '18 at 7:37






  • 1





    @ARK4579 Um, sorry, did you read the whole answer? The 2nd part gives the format you asked for (0.91123E-01 and 0.11234E-02) or am I missing something?

    – Jerry
    Nov 13 '18 at 7:39











  • yes, it does. thanks. :) but i am already using a custom function. :)

    – ARK4579
    Nov 13 '18 at 7:53

















  • yes, sorry but, that's not the output I am looking for. I am already using a custom function (in my answer)

    – ARK4579
    Nov 13 '18 at 7:37






  • 1





    @ARK4579 Um, sorry, did you read the whole answer? The 2nd part gives the format you asked for (0.91123E-01 and 0.11234E-02) or am I missing something?

    – Jerry
    Nov 13 '18 at 7:39











  • yes, it does. thanks. :) but i am already using a custom function. :)

    – ARK4579
    Nov 13 '18 at 7:53
















yes, sorry but, that's not the output I am looking for. I am already using a custom function (in my answer)

– ARK4579
Nov 13 '18 at 7:37





yes, sorry but, that's not the output I am looking for. I am already using a custom function (in my answer)

– ARK4579
Nov 13 '18 at 7:37




1




1





@ARK4579 Um, sorry, did you read the whole answer? The 2nd part gives the format you asked for (0.91123E-01 and 0.11234E-02) or am I missing something?

– Jerry
Nov 13 '18 at 7:39





@ARK4579 Um, sorry, did you read the whole answer? The 2nd part gives the format you asked for (0.91123E-01 and 0.11234E-02) or am I missing something?

– Jerry
Nov 13 '18 at 7:39













yes, it does. thanks. :) but i am already using a custom function. :)

– ARK4579
Nov 13 '18 at 7:53





yes, it does. thanks. :) but i am already using a custom function. :)

– ARK4579
Nov 13 '18 at 7:53

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53274821%2fscientific-notation-formatting-in-python%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto