How to Not Display Number as Exponent

How to suppress scientific notation when printing float values?

'%f' % (x/y)

but you need to manage precision yourself. e.g.,

'%f' % (1/10**8)

will display zeros only.

details are in the docs

Or for Python 3 the equivalent old formatting or the newer style formatting

How to not display number as exponent?

I see you've been given a format approach. Do realize that function returns a "character" value. If you wanted to globally change the behavior of your console session in favor of "whole numbers", then changing scipen option() is the way to go.

> options("scipen" = 10)
> options()$scipen
[1] 10
> 9e+08
[1] 900000000

The value given to 'scipen' is not actually the threshold for exponent format but is rather a "bias" with larger positive numbers increasing the values printed in fixed notation.

How to avoid scientific notation for large numbers in JavaScript?

There's Number.toFixed, but it uses scientific notation if the number is >= 1e21 and has a maximum precision of 20. Other than that, you can roll your own, but it will be messy.

function toFixed(x) {
if (Math.abs(x) < 1.0) {
var e = parseInt(x.toString().split('e-')[1]);
if (e) {
x *= Math.pow(10,e-1);
x = '0.' + (new Array(e)).join('0') + x.toString().substring(2);
}
} else {
var e = parseInt(x.toString().split('+')[1]);
if (e > 20) {
e -= 20;
x /= Math.pow(10,e);
x += (new Array(e+1)).join('0');
}
}
return x;
}

Above uses cheap-'n'-easy string repetition ((new Array(n+1)).join(str)). You could define String.prototype.repeat using Russian Peasant Multiplication and use that instead.

This answer should only be applied to the context of the question: displaying a large number without using scientific notation. For anything else, you should use a BigInt library, such as BigNumber, Leemon's BigInt, or BigInteger. Going forward, the new native BigInt (note: not Leemon's) should be available; Chromium and browsers based on it (Chrome, the new Edge [v79+], Brave) and Firefox all have support; Safari's support is underway.

Here's how you'd use BigInt for it: BigInt(n).toString()

Example:

const n = 13523563246234613317632;console.log("toFixed (wrong): " + n.toFixed());console.log("BigInt (right):  " + BigInt(n).toString());

Force R not to use exponential notation (e.g. e+10)?

This is a bit of a grey area. You need to recall that R will always invoke a print method, and these print methods listen to some options. Including 'scipen' -- a penalty for scientific display. From help(options):

‘scipen’: integer. A penalty to be applied when deciding to print
numeric values in fixed or exponential notation. Positive
values bias towards fixed and negative towards scientific
notation: fixed notation will be preferred unless it is more
than ‘scipen’ digits wider.

Example:

R> ran2 <- c(1.810032e+09, 4) 
R> options("scipen"=-100, "digits"=4)
R> ran2
[1] 1.81e+09 4.00e+00
R> options("scipen"=100, "digits"=4)
R> ran2
[1] 1810032000 4

That said, I still find it fudgeworthy. The most direct way is to use sprintf() with explicit width e.g. sprintf("%.5f", ran2).

Format / Suppress Scientific Notation from Pandas Aggregation Results

Granted, the answer I linked in the comments is not very helpful. You can specify your own string converter like so.

In [25]: pd.set_option('display.float_format', lambda x: '%.3f' % x)

In [28]: Series(np.random.randn(3))*1000000000
Out[28]:
0 -757322420.605
1 -1436160588.997
2 -1235116117.064
dtype: float64

I'm not sure if that's the preferred way to do this, but it works.

Converting numbers to strings purely for aesthetic purposes seems like a bad idea, but if you have a good reason, this is one way:

In [6]: Series(np.random.randn(3)).apply(lambda x: '%.3f' % x)
Out[6]:
0 0.026
1 -0.482
2 -0.694
dtype: object

How to make C++ cout not use scientific notation

Use std::fixed stream manipulator:

cout<<fixed<<"Bas ana: "<<x<<"\tSon faiz: "<<t<<"\tSon ana: "<<x+t<<endl;

Is there a way to stop Julia using scientific (exponential) notation with BigFloats?

One way to achive this, without using strings, is to convert the result and it's integer part to BigInt before do the subtract (and change the function type from BigFloat to BigInt):

function main_2(n::Int, m::Int)::BigInt
setprecision(Int(trunc((m + 3) * log2(10))))

# Calc the sqrt
result = sqrt(big(n))

# Convert the whole number to BigInt to the specified precision
sr = convert(BigInt, trunc(result*big(10)^m))

# Convert the integer part to BigInt
tr = convert(BigInt, trunc(result)*big(10)^m)

dp = sr - tr
return dp
end

Comparing the above implementation with the main_1 function, there is a little improvement:

julia> @time main_1(3, 50)
0.000042 seconds (36 allocations: 5.254 KiB)
73205080756887729352744634150587236694280525381038

julia> @time main_2(3, 50)
0.000028 seconds (51 allocations: 1.617 KiB)
73205080756887729352744634150587236694280525381038

Edit:

Other way (as commented by @Bill) is just trunc the result (to get rid of InexactError()) and change the function type to BigInt:

function main_2(n::Int, m::Int)::BigInt
setprecision(Int(trunc((m + 3) * log2(10))))
sr = sqrt(big(n))
tr = trunc(sr)
dp = (sr - tr) * big(10.0) ^ 50
return trunc(dp)
end

After testing:

julia> @time main_2(3,50)
0.000026 seconds (28 allocations: 1.016 KiB)
73205080756887729352744634150587236694280525381038

Prevent scientific notation

In your case, you're actually wanting to disable the offset. Using scientific notation is a separate setting from showing things in terms of an offset value.

However, ax.ticklabel_format(useOffset=False) should have worked (though you've listed it as one of the things that didn't).

For example:

fig, ax = plt.subplots()
ax.plot(range(2003,2012,1),range(200300,201200,100))
ax.ticklabel_format(useOffset=False)
plt.show()

Sample Image

If you want to disable both the offset and scientific notaion, you'd use ax.ticklabel_format(useOffset=False, style='plain').


Difference between "offset" and "scientific notation"

In matplotlib axis formatting, "scientific notation" refers to a multiplier for the numbers show, while the "offset" is a separate term that is added.

Consider this example:

import numpy as np
import matplotlib.pyplot as plt

x = np.linspace(1000, 1001, 100)
y = np.linspace(1e-9, 1e9, 100)

fig, ax = plt.subplots()
ax.plot(x, y)
plt.show()

The x-axis will have an offset (note the + sign) and the y-axis will use scientific notation (as a multiplier -- No plus sign).

Sample Image

We can disable either one separately. The most convenient way is the ax.ticklabel_format method (or plt.ticklabel_format).

For example, if we call:

ax.ticklabel_format(style='plain')

We'll disable the scientific notation on the y-axis:

Sample Image

And if we call

ax.ticklabel_format(useOffset=False)

We'll disable the offset on the x-axis, but leave the y-axis scientific notation untouched:

Sample Image

Finally, we can disable both through:

ax.ticklabel_format(useOffset=False, style='plain')

Sample Image

How do I print a double value without scientific notation using Java?

You could use printf() with %f:

double dexp = 12345678;
System.out.printf("dexp: %f\n", dexp);

This will print dexp: 12345678.000000. If you don't want the fractional part, use

System.out.printf("dexp: %.0f\n", dexp);

0 in %.0f means 0 places in fractional part i.e no fractional part. If you want to print fractional part with desired number of decimal places then instead of 0 just provide the number like this %.8f. By default fractional part is printed up to 6 decimal places.

This uses the format specifier language explained in the documentation.

The default toString() format used in your original code is spelled out here.



Related Topics



Leave a reply



Submit