Number Format, Writing 1E-5 Instead of 0.00001

Number format, writing 1e-5 instead of 0.00001

You can do this by converting your numbers to strings with formatting as you require, then using the argument quote = FALSE in the call to write.table.

dfr <- data.frame(x = 10^(0:15))
dfr$y <- format(dfr$x, scientific = FALSE)
write.table(dfr, file = "test.txt", quote = FALSE)

Note that you shouldn't need to change the format of the numbers in your file. Pretty much every piece of scientific software and every spreadsheet understands scientific notation for numbers, and also has number formatting options so you can view them how you choose.

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).

How to print decimal notation rather than scientific notation in Python?

p = 0.0000000000000000000001

s = str(p)

print(format(p, "." + s.split("e")[-1][1:]+"f")) if "e" in s else print(p)

Is there a way to do 5 different number formats in one cell or write VBA script to produce a number format based on the value?

Something like this will deal with modifiers:

Dim rng As Range, c As Range, op, v

Set rng = ActiveSheet.Range("A1:A100") 'for example, or = Selection

For Each c In rng.Cells
v = Trim(c.Value)
If Len(v) > 0 Then
'if the value has a modifier, remove it
op = ""
If v Like ">*" Or v Like "<*" Then
op = Left(v, 1)
v = Trim(Right(v, Len(v) - 1))
End If

If IsNumeric(v) Then
v = CDbl(v)
If v > 0.0001 And v <= 0.00099 Then
c.Value = v
c.NumberFormat = op & "[format1]" 'include any modifier in the format
ElseIf v > 0.001 And v <= 0.999 Then
c.Value = v
c.NumberFormat = op & "[format2]"
Else
'you should probably include a general format so all
' cells which might have modifiers get the same treatment
End If
'add other cases as needed
End If
End If

Next c

Where [format1] etc are the formats you got from your macro recording

Note that any modifiers are no longer part of the value of the cell after this - they exist only in the cell format. That means you can work with the cells as numbers, but depending on what you need to do eventually it might or might not be appropriate.

If you just want to change the format of the numeric part of the cell value, then you could do something like:

c.Value = Application.Text(v, op & "0.0000")

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 format double values to E format with specified exponent?

If you want this result:

    1.0      = 1.00
0.1 = 0.10
0.01 = 0.010
0.001 = 1.00e-3
0.0001 = 0.10e-3
0.00001 = 0.010e-3
0.000001 = 1.00e-6

The use this code:

class Program
{
/// <summary>
/// Format a value using engineering notation.
/// </summary>
/// <example>
/// Format("S4",-12345678.9) = "-12.34e-6"
/// with 4 significant digits
/// </example>
/// <arg name="format">The format specifier</arg>
/// <arg name="value">The value</arg>
/// <returns>A string representing the value formatted according to the format specifier</returns>
public static string Format(string format, double value)
{
if(format.StartsWith("S"))
{
string dg=format.Substring(1);
int significant_digits;
int.TryParse(dg, out significant_digits);
if(significant_digits==0) significant_digits=4;
int sign;
double amt;
int exponent;
SplitEngineeringParts(value, out sign, out amt, out exponent);
return ComposeEngrFormat(significant_digits, sign, amt, exponent);
}
else
{
return value.ToString(format);
}
}
static void SplitEngineeringParts(double value,
out int sign,
out double new_value,
out int exponent)
{
sign=Math.Sign(value);
value=Math.Abs(value);
if(value>0.0)
{
if(value>1.0)
{
exponent=(int)(Math.Floor(Math.Log10(value)/3.0)*3.0);
}
else
{
exponent=(int)(Math.Ceiling(Math.Log10(value)/3.0)*3.0);
}
}
else
{
exponent=0;
}
new_value=value*Math.Pow(10.0, -exponent);
if(new_value>=1e3)
{
new_value/=1e3;
exponent+=3;
}
if(new_value<=1e-3&&new_value>0)
{
new_value*=1e3;
exponent-=3;
}
}
static string ComposeEngrFormat(int significant_digits, int sign, double v, int exponent)
{
int expsign=Math.Sign(exponent);
exponent=Math.Abs(exponent);
int digits=v>0?(int)Math.Log10(v)+1:0;
int decimals=Math.Max(significant_digits-digits, 0);
double round=Math.Pow(10, -decimals);
digits=v>0?(int)Math.Log10(v+0.5*round)+1:0;
decimals=Math.Max(significant_digits-digits, 0);
string t;
string f="0:F";
if(exponent==0)
{
t=string.Format("{"+f+decimals+"}", sign*v);
}
else
{
t=string.Format("{"+f+decimals+"}e{1}", sign*v, expsign*exponent);
}
return t;
}

static void Main(string[] args)
{
Console.WriteLine("\t1.0 = {0}", Format("S3", 1.0));
Console.WriteLine("\t0.1 = {0}", Format("S3", 0.1));
Console.WriteLine("\t0.01 = {0}", Format("S3", 0.01));
Console.WriteLine("\t0.001 = {0}", Format("S3", 0.001));
Console.WriteLine("\t0.0001 = {0}", Format("S3", 0.0001));
Console.WriteLine("\t0.00001 = {0}", Format("S3", 0.00001));
Console.WriteLine("\t0.000001 = {0}", Format("S3", 0.000001));
}
}

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());


Related Topics



Leave a reply



Submit