Thursday, February 2, 2023
Learning Code
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#
No Result
View All Result
Learning Code
No Result
View All Result
Home Java

How to format a Java double with printf example

learningcode_x1mckf by learningcode_x1mckf
October 3, 2022
in Java
0
How to format a Java double with printf example
74
SHARES
1.2k
VIEWS
Share on FacebookShare on Twitter


How use Java printf to format a double’s decimals

To make use of Java printf to format double and float values with decimal place precision, observe these two guidelines:

  1. Use %f because the double specifier within the textual content string.
  2. Precede the letter f with a decimal and quantity to specify precision.

Java double printf instance

Right here is a straightforward instance of each a float and double being formatted with Java’s printf perform:

bundle com.mcnz.rps;

public class JavaPrintfDoubleExample 
  /* Java double printf code instance. */
  public static void important(String[] args) 
    double mint = 1234.12345;
    float  sum  = 1234.12345f;
    System.out.printf("%,.3f :: %,.5f", mint, sum);
    /* Instance prints: 1,234.123 :: 1234.12345 */
  

Java double printf perform defined

There are three key takeaways from this Java printf instance for decimal locations:

  • The %f specifier works with each double and float values.
  • The decimal and quantity after the % specify the variety of decimals to print.
  • A comma after the % signal provides a grouping separator for giant numbers.

Is the Java printf specifier %f or %d for floating-point doubles?

Many individuals confuse the %d and %f printf specifiers.

Each %d and %f are legitimate when used with the Java printf perform. Nevertheless, solely %f is meant to be used with floating level values akin to floats and doubles.

This fast chart reveals tips on how to use Java printf with double varieties.

The %d specifier is for use with non-floating level decimal whole-numbers akin to byte, quick, int and lengthy.

Java double printf syntax

The format for the Java printf perform is as follows:

% [flags] [width] [.precision] specifier-character
Non-compulsory Java double printf flags
printf flag Function
 + Will drive the quantity to be preceded by a plus or minus signal
 0 Leads to zero padding for numbers under the required width
 , The grouping separator for giant values
<area> A clean area will present a minus signal for unfavourable numbers and an area for constructive numbers

Superior Java decimal printf code instance

Here’s a extra superior Java printf format code instance with double values:

public class AdvancedJavaPrintfDoubleExample 
  /* Superior Java printf double instance */
  public static void important(String[] args) 
    double iLovePie = Math.PI * 999 * -1;
    System.out.printf("Worth is: %0,10.2f", iLovePie);
    /* This printf double instance outputs 'Worth is: -03,138.45' */
  

The output of this code instance which makes use of %0,10.3f because the format specifier is:

Worth is: -03,138.45

The %0,10.3f  format specifier breaks down like this:

  • The quantity generated totals 10 characters in width, together with the signal, radix and grouping comma.
  • Zero padding on the left ensures all 10 characters are used.
  • Precision is to 2 decimal locations.
  • A comma teams massive numbers by the hundreds.

How can Java printf format a double to 2 decimal locations?

It’s a widespread requirement to format currencies to 2 decimal locations.

You’ll be able to simply obtain this with the Java printf perform. Simply use %.2f because the format specifier. This can make the Java printf format a double to 2 decimal locations.

/* Code instance to print a double to 2 decimal locations with Java printf */
System.out.printf("I like %.2f loads!", Math.PI);

With out Java printf, double formatting is a troublesome problem. However with this straightforward methodology constructed proper into the Java API, you can also make floating level information simple to learn and simple on your customers to grasp.

Java double printf desk instance

The chart above, which reveals how completely different patterns affect the formatting of a double, was truly created with printf statements.

Right here’s the complete code:

System.out.printf("------------------------------%n");
System.out.printf(" Java double printf chartpercentn");
System.out.printf(" (quantity: 12345.12345)%n");
System.out.printf("------------------------------%n");
System.out.printf("| %-8s | %s |%n", "PATTERN", "RESULT");
System.out.printf("------------------------------%n");
System.out.printf("| %-8s | %f |%n",      "%f",      12345.12345);
System.out.printf("| %-8s | %15f |%n",    "%15f",    12345.12345);
System.out.printf("| %-8s | %-15f |%n",   "%-15f",   12345.12345);
System.out.printf("| %-8s | %-15.3f |%n", "%-15.3f", 12345.12345);
System.out.printf("| %-8s | %015.3f |%n", "%015.3f", 12345.12345);
System.out.printf("------------------------------%n");

Generally it’s enjoyable to make charts tables with Java’s printf command.

printf java double table

Right here’s the code and output of a desk of Java double printf examples.



Source link

You might also like

Java :Full Stack Developer – Western Cape saon_careerjunctionza_state

UPB Java Jam brings coffeehouse vibes to Taylor Down Under | Culture

Oracle Java Price Hike Could Be an Opportunity for OpenJDK Vendors

Share30Tweet19
learningcode_x1mckf

learningcode_x1mckf

Recommended For You

Java :Full Stack Developer – Western Cape saon_careerjunctionza_state

by learningcode_x1mckf
February 2, 2023
0
Java :Full Stack Developer – Western Cape saon_careerjunctionza_state

I’m on the lookout for a self-driven and longing for fixed self-improvement, gifted particular person to search out and be a part of their ” tribe”. On the...

Read more

UPB Java Jam brings coffeehouse vibes to Taylor Down Under | Culture

by learningcode_x1mckf
February 2, 2023
0
UPB Java Jam brings coffeehouse vibes to Taylor Down Under | Culture

The sound of acoustic guitar, delicate singing and the sturdy scent of heat espresso crammed the area of Taylor Down Beneath (TDU). Throughout the room, many individuals studied...

Read more

Oracle Java Price Hike Could Be an Opportunity for OpenJDK Vendors

by learningcode_x1mckf
February 1, 2023
0
Oracle Java Price Hike Could Be an Opportunity for OpenJDK Vendors

Inflation is rising the price of residing and doing enterprise world wide. The newest merchandise to extend in worth seems to be an Oracle Java SE subscription. Java...

Read more

Full Stack Java Developer ZN

by learningcode_x1mckf
February 1, 2023
0
Full Stack Java Developer ZN

Calling Intermediate and Senior Full Stack Java Builders! Quite a few, game-changing roles with world knowledgeable of their discipline. Modern, Agile … folks such as you .. a...

Read more

Unleash Your Coding Potential: Dive into the World of Java Syntax | by Arslan Mirza | Medium

by learningcode_x1mckf
February 1, 2023
0
Unleash Your Coding Potential: Dive into the World of Java Syntax | by Arslan Mirza | Medium

Information to Java Syntax!https://creator.nightcafe.studio/creation/pYILFuVnvg0CSMHYAUOxJava is the beating coronary heart of the digital world!From smartphones and gaming consoles to enterprise purposes and cloud computing, Java is in every single...

Read more
Next Post
Swift 5 and ABI stability

Swift 5 and ABI stability

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related News

Best Websites To Learn C++ Programming Online: Top 10

Best Websites To Learn C++ Programming Online: Top 10

September 8, 2022
Learn Python, C++, Java and more with 14 courses for $40

Learn Python, C++, Java and more with 14 courses for $40

September 25, 2022
Senior Java Developer at Reverside – Gauteng saon_careerjunctionza_state

Senior Java Developer at Reverside – Gauteng saon_careerjunctionza_state

December 3, 2022

Browse by Category

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

RECENT POSTS

  • Java :Full Stack Developer – Western Cape saon_careerjunctionza_state
  • Pay What You Want for this Learn to Code JavaScript Certification Bundle
  • UPB Java Jam brings coffeehouse vibes to Taylor Down Under | Culture

CATEGORIES

  • C#
  • C++
  • Java
  • JavaScript
  • Python
  • Swift

© 2022 Copyright Learning Code

No Result
View All Result
  • Home
  • JavaScript
  • Java
  • Python
  • Swift
  • C++
  • C#

© 2022 Copyright Learning Code

Are you sure want to unlock this post?
Unlock left : 0
Are you sure want to cancel subscription?