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.
Alex13
Posts: 2
Joined: Fri Nov 18, 2011 9:03 pm
Gender: None specified

Redondeo en JAVA

Postby Alex13 » Fri Nov 18, 2011 9:07 pm

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: Mon Nov 07, 2011 9:00 pm
Location: Universidad de las Ciencias Informáticas (UCi)
Gender: None specified

Re: Redondeo en JAVA

Postby jelara » Sat Nov 19, 2011 2:07 am

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: Fri Nov 18, 2011 9:03 pm
Gender: None specified

Re: Redondeo en JAVA

Postby Alex13 » Sun Nov 20, 2011 8:12 pm

Ok thank´s.

ldsierra
Posts: 1
Joined: Sun Dec 04, 2011 4:37 pm
Gender: None specified

Re: Redondeo en JAVA

Postby ldsierra » Sun Dec 04, 2011 4:45 pm

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: Thu Dec 08, 2011 12:02 pm
Gender: Male

Re: Redondeo en JAVA

Postby WIL » Thu Dec 08, 2011 12:08 pm

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: Mon Nov 07, 2011 9:00 pm
Location: Universidad de las Ciencias Informáticas (UCi)
Gender: None specified

Re: Redondeo en JAVA

Postby jelara » Fri Dec 09, 2011 12:01 am

@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: Fri Jan 27, 2012 11:28 am
Gender: None specified

Re: Redondeo en JAVA

Postby axlluis » Wed Jun 20, 2012 9:55 am

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

User avatar
jelara
Posts: 37
Joined: Mon Nov 07, 2011 9:00 pm
Location: Universidad de las Ciencias Informáticas (UCi)
Gender: None specified

Re: Redondeo en JAVA

Postby jelara » Tue Oct 09, 2012 1:52 pm

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: Mon Dec 30, 2013 1:18 am
Gender: None specified

Re: Redondeo en JAVA

Postby smicky125 » Mon Dec 30, 2013 1:31 am

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

jlfernandez
Posts: 1
Joined: Sun Jan 19, 2014 10:50 am
Gender: None specified

Re: Redondeo en JAVA

Postby jlfernandez » Sun Jan 19, 2014 10:57 am

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


Return to “Java”

Who is online

Users browsing this forum: No registered users and 1 guest