Skip to content
Fix Code Error

round up to 2 decimal places in java?

March 13, 2021 by Code Error
Posted By: Anonymous

I have read a lot of stackoverflow questions but none seems to be working for me. i am using math.round() to round off.
this is the code:

class round{
    public static void main(String args[]){

    double a = 123.13698;
    double roundOff = Math.round(a*100)/100;

    System.out.println(roundOff);
}
}

the output i get is: 123 but i want it to be 123.14. i read that adding *100/100 will help but as you can see i didn’t manage to get it to work.

it is absolutely essential for both input and output to be a double.

it would be great great help if you change the line 4 of the code above and post it.

Solution

Well this one works…

double roundOff = Math.round(a * 100.0) / 100.0;

Output is

123.14

Or as @Rufein said

 double roundOff = (double) Math.round(a * 100) / 100;

this will do it for you as well.

Answered By: Anonymous

Related Articles

  • Is CSS Turing complete?
  • Reference - What does this regex mean?
  • TLS 1.3 server socket with Java 11 and self-signed…
  • How can I resolve Web Component Testing error?
  • Unable to run Robolectric and Espresso with a…
  • Eclipse will not start and I haven't changed anything
  • When I'm testing a web app by JUnit and Mockito I…
  • How to convert number to words in java
  • JUNIT @ParameterizedTest , Parameter resolution Exception
  • Dynamically update values of a chartjs chart
  • How to format a phone number in a textfield
  • How to add Typescript to a Nativescript-Vue project?
  • ClassNotFoundException:…
  • Neither BindingResult nor plain target object for…
  • What is and how to fix…
  • java IO Exception: Stream Closed
  • Jetpack Compose and Hilt Conflict
  • Vue.JS & Spring Boot - Redirect to homepage on 404
  • Reference — What does this symbol mean in PHP?
  • I want to create a SQLite database like in the…
  • What is the worst programming language you ever worked with?
  • SQLException: No suitable Driver Found for…
  • SecurityException: Permission denied (missing…
  • Examples of GoF Design Patterns in Java's core libraries
  • Gradle error: Execution failed for task…
  • Requested bean is currently in creation: Is there an…
  • How can I format a decimal to always show 2 decimal places?
  • Javax.net.ssl.SSLHandshakeException:…
  • How do I install Java on Mac OSX allowing version switching?
  • Can't access Eclipse marketplace
  • Mock MQRFH2 header in JUnit Testing Error [MQRFH2…
  • Error: the entity type requires a primary key
  • What does this symbol mean in JavaScript?
  • Using enums in a spring entity
  • android studio 0.4.2: Gradle project sync failed error
  • Execution failed for task…
  • Python: Float to Decimal conversion and subsequent…
  • org.gradle.api.tasks.TaskExecutionException:…
  • How to parse JSON file with Spring
  • Active tab issue on page load HTML
  • Reduce vs Collect method on Parallel Streams vs…
  • Palindromic numbers in Java
  • How to fix Hibernate LazyInitializationException:…
  • JPA Hibernate Persistence exception…
  • Basic Question - Setting object's traits in a method…
  • How to filter a RecyclerView with a SearchView
  • How to select and view database with specific value?
  • Android- Error:Execution failed for task…
  • Android Studio 4.2.1 NullPointer Exception on startup
  • Configure hibernate to connect to database via JNDI…
  • Could not install Gradle distribution from…
  • Error inflating class android.support.v7.widget.Toolbar?
  • Launching Spring application Address already in use
  • How to convert java.util.Date to java.sql.Date?
  • Vaadin Spring Boot - There was an exception while…
  • I do not use TBB, but I get linker errors related to TBB
  • How to download and save an image in Android
  • "Non-resolvable parent POM: Could not transfer…
  • Spring Boot - Cannot determine embedded database…
  • Round a double to 2 decimal places
  • ClassNotFoundException thrown
  • org.springframework.beans.factory.NoSuchBeanDefiniti…
  • java.lang.IllegalStateException: Can not perform…
  • Spring Boot with ElasticSearch in Groovy: WebClient…
  • NullpointerException error while working with…
  • Simplest way to create Unix-like continuous pipeline…
  • Java ElasticSearch None of the configured nodes are…
  • Problem while using user insert for getter
  • Getting Dropwizard Client And Jersey/HTTP I/O Error…
  • Jackson cannot parse body of a POST request
  • How do I use 3DES encryption/decryption in Java?
  • Is there a reason why it doesn't go to the 'else if'…
  • 3D Rotation of a camera using its own, new axes
  • How do I list / export private keys from a keystore?
  • Item position in RecyclerView only changing when…
  • How do you easily create empty matrices javascript?
  • Eclipse fails to open .vue files in Web Page Editor
  • java.lang.ClassNotFoundException: HttpServletRequest
  • After a little scroll, the sticky navbar just is not…
  • Android - How to download a file from a webserver
  • Plugin…
  • Fastest way to iterate over all the chars in a String
  • UnsatisfiedDependencyException: Error creating bean…
  • org.springframework.beans.factory.BeanCreationExcept…
  • What does "Could not find or load main class" mean?
  • "The public type must be defined in its own file"…
  • java.net.UnknownHostException: Unable to resolve…
  • Where is the mistake? I wanted to add the ability to…
  • Injection of autowired dependencies failed;
  • java.lang.ClassNotFoundException:…
  • How to truncate float values?
  • How to draw in JPanel? (Swing/graphics Java)
  • 'foo' was not declared in this scope c++
  • How can I throw CHECKED exceptions from inside Java…
  • What is a stack trace, and how can I use it to debug…
  • Fix top buttons on scroll of list below
  • java.lang.RuntimeException: Unable to instantiate…
  • Parcelable encountered IOException writing…
  • Draw in Canvas by finger, Android
  • Authenticating against Active Directory with Java on Linux

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

Convert Date format into DD/MMM/YYYY format in SQL Server

Next Post:

What is a serialVersionUID and why should I use it?

Leave a Reply Cancel reply

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

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error