Round off a double while maintaining the trailing zero

Round off a double while maintaining the trailing zero



Here is my function to roundoff a number upto two decimals but when the rounded off number is 1.50 it seems to ignore the trailing zero and just returns 1.5


public static double roundOff(double number)
double accuracy = 20;
number = number * accuracy;
number = Math.ceil(number);
number = number / accuracy;
return number;



So if I send 1.499 it returns 1.5 where as I want 1.50





There isn't a separate double for 1.5 and for 1.50. They're numerically the same, so there's only one double representation of them both.
– Dawood ibn Kareem
Jan 8 '15 at 2:48



double


double





Maybe your method should return String, rather than double, so that you can do whatever kind of formatting you want with it.
– Dawood ibn Kareem
Jan 8 '15 at 2:50


String


double





Are you sure about that 20? Maybe I misunderstood what you are trying to do, but your method for 1.489 wouldn't return 1.49. I would think you'd want to set accuracy to 100 for two decimal places.
– cyon
Jan 8 '15 at 2:59


20


1.489


1.49


100





@cyon, it should round it up to the next multiple of 1/20, so 1.499 will indeed become 1.5. But you're right that it doesn't gel with the "up to two decimals" comment.
– paxdiablo
Jan 8 '15 at 3:06





4 Answers
4



This is a printing poblem:


double d = 1.5;
System.out.println(String.format("%.2f", d)); // 1.50





Got it. Thanks. This helped.
– Nick Div
Jan 8 '15 at 2:53



1.5 is, number of significant digits notwithstanding, the same as 1.50 (and even 1.5000000000000).


1.5


1.50


1.5000000000000



You need to separate the value of the number from its presentation.



If you want it output with two decimal digits, just use String.format, such as with:


String.format


public class Test

public static void main(String args)
double d = 1.50000;
System.out.println(d);
System.out.println(String.format("%.2f", d));




which outputs:


1.5
1.50



If you still want a function that does all that for you and gives you a specific format, you'll need to return the string with something like:


public static String roundOff(double num, double acc, String fmt)
num *= acc;
num = Math.ceil(num);
num /= acc;
return String.format(fmt, num);



and call it with:


resultString = roundOff(value, 20, "%.2f"); // or 100, see below.



This will allow you to tailor the accuracy and output format in any way you desire, although you can still hard-code the values if you want simplicity:


public static String roundOff(double num)
double acc = 20;
String fmt = "%.2f";
num *= acc;
num = Math.ceil(num);
num /= acc;
return String.format(fmt, num);



One final note: your question states that you want to round to "two decimals" but that doesn't quite gel with your use of 20 as the accuracy, since that will round it up to the next multiple of 1/20. If you really want it rounded up to two decimals, the value you should be using for accuracy is 100.


20


1/20


accuracy


100





Thanks, for the detailed explanation.
– Nick Div
Jan 8 '15 at 3:16





In your last little snippet, you have two different variables called acc (one parameter, one local) and two different variables called fmt. Then you use a variable called accuracy which appears not to exist. You might want to fix this.
– Dawood ibn Kareem
Jan 8 '15 at 3:23



acc


fmt


accuracy





@David, damn those cut'n'paste errors :-) Thanks for that, fixed now.
– paxdiablo
Jan 8 '15 at 4:58





Sorry, I really should have just edited it myself. It was clear from the words that you meant those two variables to be locals, not parameters.
– Dawood ibn Kareem
Jan 8 '15 at 5:00



You will have to format it as a String in order to do that. Java, like most languages, will drop the trailing zero.


String


String.format("%.2f", number);



So you can either return a String (change your return type from double) or just format it when you need to display it using the code above. You can read the JavaDoc for Formatter in order to understand all of the possibilities with number of decimal places, comma placement, etc.


String





Thanks, Jean's solution which is the same as your's worked smoothly.
– Nick Div
Jan 8 '15 at 2:54





Just one note, though it doesn't affect the output: the 1 in your format string has little effect here since it sets the minimum field width for the whole thing. Since you're also specifying it be of the form .99, it already has to be more than that.
– paxdiablo
Jan 8 '15 at 2:56


1


.99





Thanks @paxdiablo. I had it as something different (formatted with comma separator which I use personally) and then changed it to be simpler and in my haste left the "1." in. Corrected now, thank you!
– Todd
Jan 8 '15 at 2:57




You can try this if you want is a String output


double number = roundOff(1.499);//1.5

DecimalFormat decimalFormat = new DecimalFormat("#.00");
String fromattedDouble = decimalFormat.format(number);//1.50



The function roundOff is the same as you mentioned in your question.



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế