nitlang / nit

Nit language
http://nitlanguage.org
Apache License 2.0
239 stars 65 forks source link

Abstract_text: Add a method to get string representation of float in scientific notation #2785

Closed Delja closed 5 years ago

Delja commented 5 years ago

Add a method to_se to get the string representation of float in scientific notation.

123.45.to_se == "1.2345e+02"
0.001234.to_se  == "1.234e-03"
10860460000.0.to_se == "1.086046e+10"

The to_se method have a precision between 1 to 6 max decimal. The precision is adapted in function of the number.

Add a to_precision_size_with_format and to_precision_fill_with_format method to get the representation in a specific given format

Delja commented 5 years ago

To my knowledge, there is no language that specifically provides a method for returning a string with scientific notation. Generally the conversion is done using a user-specified format. The best solution would be to define a more verbose name. Example to_s_expo_notation or something else.

Morriar commented 5 years ago

https://python-reference.readthedocs.io/en/latest/docs/float/scientific.html

Delja commented 5 years ago

@Morriar Your link it's for returns a float multiplied by the specified power. The objective of this pr is to provide a method to return a string representing a float in a scientific notation.

lbajolet commented 5 years ago

May I suggest something like to_sci for the name? Bit more concise than to_scientific_notation, and slightly more explicit than to_se in my eyes

Delja commented 5 years ago

@R4PaSs nice, the to_sci look good, but to_scientific_notation it's also correct. @privat what do you prefers?

privat commented 5 years ago

+0.1e1 for to_sci