How to round a number[double] to 4 decimal places

  • Thread starter Thread starter sunilkamadolli
  • Start date Start date
Click For Summary

Discussion Overview

The discussion revolves around the problem of rounding a double number to four decimal places in Java programming. Participants share various algorithms and methods they have attempted, seeking effective solutions.

Discussion Character

  • Technical explanation, Homework-related

Main Points Raised

  • One participant expresses difficulty in rounding a double to four decimal places using Java's round function, which only rounds to one decimal place.
  • Another participant suggests dividing the number by 10000 as a potential solution.
  • A different participant proposes multiplying the number by 1000, applying the round function, and then dividing by 1000 as a method to achieve the desired rounding.
  • A later reply acknowledges the previous suggestion and indicates that the correct order of operations is to round first and then divide by 1000.
  • A participant shares a link to a webpage containing relevant code for rounding.

Areas of Agreement / Disagreement

Participants present multiple approaches to rounding, with no consensus on a single method being established as the best solution.

Contextual Notes

Some methods discussed may depend on specific programming contexts or assumptions about the input values, and the effectiveness of each approach may vary based on these factors.

sunilkamadolli
Messages
41
Reaction score
0
hey everybody, i was writing a java program and i could not figure out how to round a number[double] to 4 decimal places...i tried a lot of things

the math function's round function rounds the double to one decimal place so that does not work.

i tried various algorithms like dividing by .0001 and then rounded it and stored it into an integer and then divided the integer by 1000...still not working

please help me out...even non-java programmers can help by telling me the algorithm...thank u.
 
Computer science news on Phys.org
sunilkamadolli said:
i tried various algorithms like dividing by .0001 and then rounded it and stored it into an integer and then divided the integer by 1000...still not working

Divide by 10000..
 
yeah its kinda crazy. what you have to do is multiply that number by 1000, do the round function and then divide by 1000
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
4K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
9
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 1 ·
Replies
1
Views
7K
Replies
12
Views
4K
  • · Replies 4 ·
Replies
4
Views
2K