Printf

3 respostas
Alkamavo

Boas malta..

pretendo fazer o printf de um vector de objectos do tipo Point com 2 casas decimais apenas.

a classe que define um ponto é:
package poo;

import java.awt.Graphics;

public class Point {
	private double x;
	private double y;

	static int diameter = 3;

	public Point() {
		x = 0.0;
		y = 0.0;
	}


	public Point(double x, double y) {
		this.x = x;
		this.y = y;
	}

	
	public Point(Point p) {
		x = p.x;
		y = p.y;
	}

	
	public Point clone() {
		return new Point(this);
	}

	

	public boolean equals(Object other) {
		if (other == null)
			return false;
		if (getClass() != other.getClass())
			return false;
		Point p = (Point) other;
		return this.x == p.x && this.y == p.y;
	}

	
	public void setX(double x) {
		this.x = x;
	}

	
	public double getX() {
		return x;
	}

	
	public void setY(double y) {
		this.y = y;
	}

	
	public double getY() {
		return y;
	}

	/**
	 * Sets the fields to the corresponding arguments
	 * 
	 * @param x
	 *            the new x-coordinate.
	 * @param y
	 *            the new y-coordinate.
	 */
	public void set(double x, double y) {
		this.x = x;
		this.y = y;
	}

	/**
	 * Translates the object by dx horizontally and dy vertically.
	 * 
	 * @param dx
	 *            the horizontal displacement
	 * @param dy
	 *            the vertical displacement
	 */
	public void translate(double dx, double dy) {
		x += dx;
		y += dy;
	}

	/**
	 * Scales the object with factor fx horizontally and factor fy vertically.
	 * 
	 * @param fx
	 *            the horizontal scale factor.
	 * @param fy
	 *            the vertical scale factor.
	 */
	public void scale(double fx, double fy) {
		x *= fx;
		y *= fy;
	}

	/**
	 * Rotates the object by the angle, counterclockwise.
	 * 
	 * @param angle
	 *            the angle by which the point is rotated, expressed in radians.
	 */
	public void rotate(double angle) {
		double x0 = x;
		double y0 = y;
		x = x0 * Math.cos(angle) - y0 * Math.sin(angle);
		y = x0 * Math.sin(angle) + y0 * Math.cos(angle);
	}

	/**
	 * Computes the Euclidean distance from the object to the argument.
	 * 
	 * @param other
	 *            the point to which the distance is computed.
	 * @return the computed distance.
	 */
	public double distanceTo(Point other) {
		return Math.sqrt(Math.pow(x - other.x, 2) + Math.pow(y - other.y, 2));
	}

	/**
	 * Computes the angle made from the origin to the object and the positive
	 * x-axis, counterclockwise.
	 * 
	 * @return the angle, expressed in radians.
	 */
	public double angle() {
		return Math.atan2(y, x);
	}

	/**
	 * Computes the modulus of the object, i.e., the distance to the origin.
	 * 
	 * @return the modulus.
	 */
	public double modulus() {
		return Math.sqrt(x * x + y * y);
	}

	/**
	 * Computes the string represation of the object, overring the inherited
	 * method.
	 * 
	 * @return the string representation of the object.
	 */
	public String toString() {
		return " Point " + x + " " + y;

	}

	/**
	 * Draws the point on the surface represented by the argument.
	 * 
	 * @param g
	 *            the surface where the point is drawn.
	 */
	public void draw(Graphics g) {
		// g.fillOval((int)Math.round(x-1), (int)Math.round(y-1), 3, 3);
		g.fillOval((int) Math.round(x) - diameter / 2, (int) Math.round(y)
				- diameter / 2, diameter, diameter);
	}

	/**
	 * Getter for the static diameter of the point when drawn.
	 * 
	 * @return the diameter.
	 */
	public static int getDiameter() {
		return diameter;
	}

	public static void setDiameter(int diameter) {
		Point.diameter = diameter + 1 - diameter % 2;
	}

	public void PrintShit() {
		System.out.printf("%2f ", x);
		System.out.printf("%2f%n", y);

	}

}
este metodo :
public void PrintShit() {
		System.out.printf("%2f ", x);
		System.out.printf("%2f%n", y);

	}
é o k chamo para me fazer o printf de um ponto com apenas 2 casas decimais... infelizmente ele me faz um printf com seis casas decimais...

0,000000 0,000000
0,200000 -0,160000
0,400000 -0,240000
0,600000 -0,240000
0,800000 -0,160000
1,000000 0,000000
1,200000 0,240000
1,400000 0,560000
1,600000 0,960000
1,800000 1,440000
2,000000 2,000000

o vector é criado neste metodo

public Point[] plot(double Limit_inferior, double Limit_superior, int n) {
		Point[] xp = new Point[n + 1];
		Parabola X = new Parabola(this.a, this.b, this.c);

		double inter_iguais = (Limit_superior - Limit_inferior) / n;

		double aux = Limit_inferior;
		for (int j = 0; j < xp.length; j++) {
			xp[j] = new Point(aux, X.value(aux));
			aux += inter_iguais;

		}
		return xp;
	}
}

help

3 Respostas

Guitar_Men

procure algo sobre numberFormat, acho que vc consegue formatar o tipo double para quantas casas decimais vc quiser…

peczenyj

teste uma dessas opções de formatação

%.2f
%.02f

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax

Veja o que esse link fala sobre precision.

Alkamavo

peczenyj:
teste uma dessas opções de formatação

%.2f
%.02f

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Formatter.html#syntax

Veja o que esse link fala sobre precision.


testado e resolvido…
tava é me eskecendo de por um ponto depois do “%” que nobice a minha…

Criado 10 de março de 2008
Ultima resposta 10 de mar. de 2008
Respostas 3
Participantes 3