It looks like %f works just fine:

#include <stdio.h>

int main()
{
  double d = 5000683;
  printf("%f\n", d);
  printf("%.0f\n", d);

  return 0;
}

The output of this code will be

5000683.000000
5000683

The second printf() statement sets the precision to 0 (by prefixing f with .0) to avoid any digits after the decimal point.

Answer from Striezel on Stack Overflow
Discussions

How do I print a double value without scientific notation using Java? - Stack Overflow
0 I want to print amount like 2523525252.025 when I using double data type it's changed in 2.523525252025E9 how can fix? 0 Why a double division is returning a very wrong value? -1 Properly converting a double variable to string · 1 How do I convert scientific notation to regular floating value. More on stackoverflow.com
🌐 stackoverflow.com
How to print the char, integer, float and double values without using format specifiers in c - Stack Overflow
I want to print the char, int, float and double values without using format specifiers in my c program. I can able to print the string using the below code: char s[] = "Hello\n"; fprintf(stdout, s... More on stackoverflow.com
🌐 stackoverflow.com
c - Printing a double without the decimal places - Stack Overflow
I have a number stored as a double and want to print it without the decimal places. So if I had the double value 919545634521.000000, it is always printed with the decimal places added to it. How... More on stackoverflow.com
🌐 stackoverflow.com
How can I get cout to print large numbers that aren't in scientific notation in C++?
http://stackoverflow.com/questions/5212018/making-c-cout-not-use-scientific-notation Use std::fixed stream manipulator More on reddit.com
🌐 r/learnprogramming
6
1
June 23, 2015
Top answer
1 of 3
4

As mentioned in the comments of the other answer, your decimal floating point numbers are stored as binary and rarely have finite representations.

If you simply want code that passes your test I would mention that it is much easier to remove padding from std::fixed than it is to expand the scientific notation.

#include <iostream>
#include <string>
#include <sstream>
#include <limits>
#include <iomanip>
#include <cstdlib>
#include <cassert>
#include <cstddef>

static std::string to_string(double d)
{
    std::ostringstream oss;
    oss.precision(std::numeric_limits<double>::digits10);
    oss << std::fixed << d;
    std::string str = oss.str();

    // Remove padding
    // This must be done in two steps because of numbers like 700.00
    std::size_t pos1 = str.find_last_not_of("0");
    if(pos1 != std::string::npos)
        str.erase(pos1+1);

    std::size_t pos2 = str.find_last_not_of(".");
    if(pos2 != std::string::npos)
        str.erase(pos2+1);

    return str;
}


static void test_string_conversion(const std::string& str)
{
    assert( to_string( atof(str.c_str()) ) == str );
}


int main(void)
{
    test_string_conversion( "1.5203" );
    test_string_conversion( "-1.025" );
    test_string_conversion( "3.1415" );
    test_string_conversion( "-38271" );
    test_string_conversion( "0.8382" );
    test_string_conversion( "-0.8382" );
    test_string_conversion( "7" );
    test_string_conversion( "-3" );
    test_string_conversion( "0.0000024" );
    test_string_conversion( "7300000000" );
}
2 of 3
1

What you are looking for is std::fixed

#include <iostream>
#include <iomanip>

int main()
{
   std::cout << std::fixed << 1.5203 << "\n";
   std::cout << std::fixed << -1.025 << "\n";
   std::cout << std::fixed << 3.1415 << "\n";
   std::cout << std::fixed << -38271 << "\n";
   std::cout << std::fixed << 0.8382 << "\n";
   std::cout << std::fixed << -0.8382 << "\n";
   std::cout << std::fixed << 7 << "\n";
   std::cout << std::fixed << -3 << "\n";
   std::cout << std::fixed << 0.0000024 << "\n";
   std::cout << std::fixed << 7300000000 << "\n";
}

1.520300
-1.025000
3.141500
-38271
0.838200
-0.838200
7
-3
0.000002
7300000000
Top answer
1 of 16
321

Java prevent E notation in a double:

Five different ways to convert a double to a normal number:

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class Runner {
    public static void main(String[] args) {
        double myvalue = 0.00000021d;

        //Option 1 Print bare double.
        System.out.println(myvalue);

        //Option2, use decimalFormat.
        DecimalFormat df = new DecimalFormat("#");
        df.setMaximumFractionDigits(8);
        System.out.println(df.format(myvalue));

        //Option 3, use printf.
        System.out.printf("%.9f", myvalue);
        System.out.println();

        //Option 4, convert toBigDecimal and ask for toPlainString().
        System.out.print(new BigDecimal(myvalue).toPlainString());
        System.out.println();

        //Option 5, String.format 
        System.out.println(String.format("%.12f", myvalue));
    }
}

This program prints:

2.1E-7
.00000021
0.000000210
0.000000210000000000000001085015324114868562332958390470594167709350585
0.000000210000

Which are all the same value.

Protip: If you are confused as to why those random digits appear beyond a certain threshold in the double value, this video explains: computerphile why does 0.1+0.2 equal 0.30000000000001?

http://youtube.com/watch?v=PZRI1IfStY0

2 of 16
151

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.

🌐
Blogger
self-learning-java-tutorial.blogspot.com › 2020 › 03 › how-to-print-double-value-without.html
Programming for beginners: How to print double value without scientific notation?
March 27, 2020 - There are multiple ways to print a double without scientific notation. Approach1: Using System.out.ptintf. System.out.printf("d: %f\...
Top answer
1 of 4
4

To print a char, use:

fputc(c, stream)

(If the stream is stdout, you can use putchar(c).)

To print an int:

  • If the int is negative, print “-”.
  • Calculate the individual digits of the integer. This can be done either by calculating the digits from least significant to most significant and saving them in a buffer to be printed in reverse order or by figuring out where the most significant digit is and then calculating the digits from most significant to least significant. You can use a remainder operation, such as x % 10, to calculate the least significant digit of a number, and you can use division, such as x / 10, to remove that digit.
  • One caveat is that, if the original number is negative, you have to be careful about calculating its digits. The % operator will return negative values. Some people attempt to deal with this by negating the integer if it is negative. However, if the number is the least possible int, this may overflow. E.g., in many C implementations, the least int value is -2,147,483,648, but it cannot be negated because the greatest int is 2,147,483,647.
  • Any digit in numeric form (0 to 10) can be converted to a character (“0” to “9”) by adding '0', such as int d = x % 10; char c = d + '0';. The C standard guarantees that this produces the appropriate character in c.
  • After you obtain the characters from the digits, print them.

To print a float or double:

  • Doing this completely correctly is hard, although it is a solved problem. The classic reference for it is Correctly Rounded Binary-Decimal and Decimal-Binary Conversions by David M. Gay.
  • If you just want a simple implementation suitable for a learning exercise, then you can format a floating-point value much as you would an integer: Calculate the digits individually. You also need to decide whether to print a fixed-point notation or a scientific notation (or other).
  • To print a fixed-point notation, print the integer part of the value as above, for integer types. Then print a “.” and some digits for the fractional part of the value.
  • To print a scientific notation, calculate the value of the exponent part (e.g., to express 12345789 as “1.23456789e7”, the exponent is 7, for 107. Divide the value by 10 raised to the power of that exponent and print the resulting value as a fixed-point number (so, in this example, you print “1.23456789”), then print “e”, the print the exponent part.
  • Floating-point rounding errors will occur in the above, making it suitable only for a learning exercise, not for use in a quality product.

The above should suffice to get you started. It is not complete code, obviously.

2 of 4
1

just one thought, not very optimal:

int myvalue = 12345;
char buffer[100];
size_t index = 0;

while (myvalue) {
  buffer[index] = '0' + myvalue % 10;
  myvalue = myvalue / 10;
  index++;
}
buffer[index] = '\0';

reverse(buffer);
fprintf(stdout, buffer);

you have to consider the negative sign. And the sizeof buffer (100 is a very bad guess).

🌐
Baeldung
baeldung.com › home › java › java numbers › print a double value without scientific notation in java
Print a Double Value Without Scientific Notation in Java | Baeldung
December 28, 2023 - In our daily tasks, we may often come across challenges to format double values. One such challenge could be printing a double value, avoiding scientific notation. Even though this method helps us express large and small values more compactly, there are situations where the default scientific notation might not be the most practical choice.
Find elsewhere
🌐
CodingTechRoom
codingtechroom.com › question › -print-floating-point-without-scientific-notation
How to Print Floating Point Values in Standard Decimal Format Without Mantissa or Exponent - CodingTechRoom
By default, some languages may ... # Print with two decimal places print(f'{value:.2f}') # Example in C/C++ double value = 123456.789; printf('%.2f', value);...
🌐
Cplusplus
cplusplus.com › forum › beginner › 249091
How to show output NOT in scientific notation - Forum
One: My output is being displayed in scientific notation, and I would like it not to be. Two: One of my values is not showing the same return as the sample solution. Any direction would be greatly appreciated. ... Hello al bama cpp, By putting a green check on your post you are telling everyone that you have an answer and are finished. I believe that you can use the "edit" button to change the icon. Next "double" is the preferred floating point type these days.
Top answer
1 of 16
321

Java prevent E notation in a double:

Five different ways to convert a double to a normal number:

import java.math.BigDecimal;
import java.text.DecimalFormat;

public class Runner {
    public static void main(String[] args) {
        double myvalue = 0.00000021d;

        //Option 1 Print bare double.
        System.out.println(myvalue);

        //Option2, use decimalFormat.
        DecimalFormat df = new DecimalFormat("#");
        df.setMaximumFractionDigits(8);
        System.out.println(df.format(myvalue));

        //Option 3, use printf.
        System.out.printf("%.9f", myvalue);
        System.out.println();

        //Option 4, convert toBigDecimal and ask for toPlainString().
        System.out.print(new BigDecimal(myvalue).toPlainString());
        System.out.println();

        //Option 5, String.format 
        System.out.println(String.format("%.12f", myvalue));
    }
}

This program prints:

2.1E-7
.00000021
0.000000210
0.000000210000000000000001085015324114868562332958390470594167709350585
0.000000210000

Which are all the same value.

Protip: If you are confused as to why those random digits appear beyond a certain threshold in the double value, this video explains: computerphile why does 0.1+0.2 equal 0.30000000000001?

http://youtube.com/watch?v=PZRI1IfStY0

2 of 16
151

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.

Top answer
1 of 16
59

For a general-purpose¹ solution you need to preserve 339 places:

doubleValue.ToString("0." + new string('#', 339))

The maximum number of non-zero decimal digits is 16. 15 are on the right side of the decimal point. The exponent can move those 15 digits a maximum of 324 places to the right. (See the range and precision.)

It works for double.Epsilon, double.MinValue, double.MaxValue, and anything in between.

The performance will be much greater than the regex/string manipulation solutions since all formatting and string work is done in one pass by unmanaged CLR code. Also, the code is much simpler to prove correct.

For ease of use and even better performance, make it a constant:

public static class FormatStrings
{
    public const string DoubleFixedPoint = "0.###################################################################################################################################################################################################################################################################################################################################################";
}

¹ Update: I mistakenly said that this was also a lossless solution. In fact it is not, since ToString does its normal display rounding for all formats except r. Live example. Thanks, @Loathing! Please see Lothing’s answer if you need the ability to roundtrip in fixed point notation (i.e, if you’re using .ToString("r") today).

2 of 16
39

I had a similar problem and this worked for me:

doubleValue.ToString("F99").TrimEnd('0')

F99 may be overkill, but you get the idea.

🌐
Cprogramming
cprogramming.com › tutorial › floating_point › understanding_floating_point_printing.html
Printing floating point numbers - Cprogramming.com
How to begin Get the book · C tutorial C++ tutorial Game programming Graphics programming Algorithms More tutorials