Redondeo en JAVA

Discussion on Java at the COJ. This is the place to clear your doubts about these languages, and to share with the community the new things you learn about them.
Post Reply
Alex13
Posts: 2
Joined: 6 years ago
Gender: None specified

Redondeo en JAVA

Post by Alex13 » 6 years ago

Hola quisiera saber como puedo redondear un numero a n lugares luego del punto en java. :?:
Yo utilizo Math.rint(x*cant_cifras)/cant_cifras, pero por ejemplo si redondeo 3.0000 lo lleva a 3.0



User avatar
jelara
Posts: 37
Joined: 6 years ago
Location: Universidad de las Ciencias Informáticas (UCi)
Gender: None specified

Re: Redondeo en JAVA

Post by jelara » 6 years ago

Hi Alex,

For this, you can use String.format, wich can be used with a format very similar to C++'s printf: you pass a format string and a number, and it returns a string containing the number's representation in the desired format.

For example, if you want 4 figures after the decimal point, you would use:

Code: Select all

float result = 3.0f;
String output = String.format("%.4f", result);
System.out.println(output);
which would effectively display "3.0000" on the console.

I invite you to lookup the Javadocs for the String.format function, specifically the format string, so you can harness the power of this function and add it to your contestant's toolkit. Here is the URL:

http://download.oracle.com/javase/6/doc ... tml#syntax

By the way, remember to post only in English: not all of our users are Spanish-speakers!

Alex13
Posts: 2
Joined: 6 years ago
Gender: None specified

Re: Redondeo en JAVA

Post by Alex13 » 6 years ago

Ok thank´s.

ldsierra
Posts: 1
Joined: 6 years ago
Gender: None specified

Re: Redondeo en JAVA

Post by ldsierra » 6 years ago

Hi Alex13
You can also use this way

Code: Select all

System.out.printf("%.2f", 12.35642);     //output  12,36
System.out.printf("%.5f", 12.35);        //output  12,35000

User avatar
WIL
Posts: 11
Joined: 6 years ago
Gender: Male

Re: Redondeo en JAVA

Post by WIL » 6 years ago

Otra manera de redondear pero que no pone los 0s a la derecha es:

Code: Select all

double num=8.122800;
System.out.println(Math.ring(num*100)/100);
Esto redondea 2 lugares despues de la coma!!!
I'm interesting in learn!!!

User avatar
jelara
Posts: 37
Joined: 6 years ago
Location: Universidad de las Ciencias Informáticas (UCi)
Gender: None specified

Re: Redondeo en JAVA

Post by jelara » 6 years ago

@WIL: That is like the code posted by Alex13, but he was precisely asking for a way to obtain the zeroes on the right.
@ldsierra: Wow, thanks! I haven't noticed there was an actual printf function :)

axlluis
Posts: 17
Joined: 6 years ago
Gender: None specified

Re: Redondeo en JAVA

Post by axlluis » 6 years ago

Hi , how can i do the same but in c#??

User avatar
jelara
Posts: 37
Joined: 6 years ago
Location: Universidad de las Ciencias Informáticas (UCi)
Gender: None specified

Re: Redondeo en JAVA

Post by jelara » 6 years ago

axlluis wrote:Hi , how can i do the same but in c#??
Oh, in C# this is a piece of cake. The float.ToString function takes a parameter which controls the formatting... take a look at MSDN and all your doubts will be cleared.

smicky125
Posts: 1
Joined: 4 years ago
Gender: None specified

Re: Redondeo en JAVA

Post by smicky125 » 4 years ago

Heloo alex it is agreat information you share thanks lot all of you for the great help :)
FSL:

jlfernandez
Posts: 1
Joined: 4 years ago
Gender: None specified

Re: Redondeo en JAVA

Post by jlfernandez » 4 years ago

Hello, using the Format function is a great idea, i always have problem with that, thank...

Post Reply

Return to “Java”