Yo utilizo Math.rint(x*cant_cifras)/cant_cifras, pero por ejemplo si redondeo 3.0000 lo lleva a 3.0
Redondeo en JAVA
Redondeo en JAVA
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
Yo utilizo Math.rint(x*cant_cifras)/cant_cifras, pero por ejemplo si redondeo 3.0000 lo lleva a 3.0
Re: Redondeo en JAVA
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:
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!
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);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!
Re: Redondeo en JAVA
Hi Alex13
You can also use this way
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
Re: Redondeo en JAVA
Otra manera de redondear pero que no pone los 0s a la derecha es:
Esto redondea 2 lugares despues de la coma!!!
Code: Select all
double num=8.122800;
System.out.println(Math.ring(num*100)/100);I'm interesting in learn!!!
Re: Redondeo en JAVA
@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
@ldsierra: Wow, thanks! I haven't noticed there was an actual printf function
Re: Redondeo en JAVA
Hi , how can i do the same but in c#??
Re: Redondeo en JAVA
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.axlluis wrote:Hi , how can i do the same but in c#??
Re: Redondeo en JAVA
Heloo alex it is agreat information you share thanks lot all of you for the great help 
FSL:
-
jlfernandez
- Posts: 1
- Joined: 7 years ago
- Gender:

Re: Redondeo en JAVA
Hello, using the Format function is a great idea, i always have problem with that, thank...
