It's not a secret anymore!
The moose likes Beginning Java and the fly likes Problem in Decimal Percent Formating Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem in Decimal Percent Formating" Watch "Problem in Decimal Percent Formating" New topic

Problem in Decimal Percent Formating

Gagan Singh

Joined: Dec 05, 2006
Posts: 2
I m trying to make a method in which i want to format a fraction into percent format.
But i m unable to remove th errors in it.
If som1 can help me out...
below is the code i have written...

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.MessageFormat;
import java.text.NumberFormat;

public class formating {

public static void main(String[] args) {

BigDecimal number = 8.375; // This is the value I want to format
public static final String DEFAULT_PERCENT_FORMAT = "#0.0#";

public String formatPercent(BigDecimal amount) { // Its showing syntax error here

DecimalFormat format = null;
format = new DecimalFormat(getProperty(DEFAULT_PERCENT_FORMAT));
return format.format(amt);

Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15084

First of all, please use code tags when you post code, so that the forum automatically formats your code properly so that it's much easier to read.

You get the syntax error because your placement of { and } braces is wrong. You are now declaring a public static final member variable and a method inside your main() method. You can't do that. Those things have to be at class level, not inside the main() method. There are other errors as well, for example, what is getProperty()? You don't have a getProperty() method in your class.

It should look like this (note, I didn't test if this compiles):

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: Problem in Decimal Percent Formating
jQuery in Action, 3rd edition