Jump to content

Number format

- - - - -

  • Please log in to reply
6 replies to this topic

#1
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
Hello,

I have problem with a decimal number format.

Here is my code

NumberFormat numberFormatter = new DecimalFormat("#.##");

	String amount = numberFormatter.format(0.00);

but when running the program, TextField shows just 0. I have a mechanism that increases the number by 0.5, so when increase it shows 0.5 and not 0.50. How I can fix it?

Thank you
toto_7

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#2
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Use '0' instead of '#', '#' won't show zeros when it can.
NumberFormat numberFormatter = new DecimalFormat("0.00");


#3
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
That works for first one thank you :) . But increase still have the same problem, goes 0.5 - 1.0 - 1.5 - 2.0 ...

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#4
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Do you format the number again after increasing?

#5
toto_7

toto_7

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 295 posts
No. I thought that format remains after that

"Programming is like sex. One mistake and you have to support it for the rest of your life."

-Michael Sinz

#6
wim DC

wim DC

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 2,084 posts
  • Programming Language:Java, JavaScript, PL/SQL
  • Learning:Java
Nah, you just format the string, not the actual number

double number = 7.1654
NumberFormat numberFormatter = new DecimalFormat("#.##");
String amount = numberFormatter.format(number);

amount will contain "7.17", but number still has 7.1654 in it. Every time you want to display the number you'll have to format it.

You can consider creating a class which holds the number, and override its toString method to always use the formatter

#7
chili5

chili5

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 7,247 posts
  • Programming Language:Java, C#, PHP
  • Learning:C, C++, C#, PHP, Transact-SQL, Assembly, Scheme
A class that does that would look like this:

class DoubleFormat {
    private double _val;
    private static NumberFormat _format = new DecimalFormat("0.00");
    
    public DoubleFormat(double val) {
        _val = val;
    }

    public double getValue() {
        return _val;
    }

    public void setValue(double _val) {
        this._val = _val;
    }
    
    @Override
    public String toString() {
        return _format.format(_val);
    }
}

Use it like this:

public static void main(String[] args) {
        DoubleFormat format = new DoubleFormat(3.45678);
        System.out.println(format);
    }





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users