Info

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

/**
 * @author King
 *
 */
public class DecimalFormatTest {

    public static void main(String[] args) {
        NumberFormat nemf = NumberFormat.getPercentInstance(Locale.JAPAN);
        DecimalFormat decf = (DecimalFormat)nemf;
        decf.applyPattern(“#.###”);
       
        double d1 = 1.0015;
        System.out.println(d1+ “=>” + decf.format(d1));
       
        double d2 = 1.0025;
        System.out.println(d2+ “=>” + decf.format(d2));
    }
}

処理結果は以下のようになります。

1.0015=>1.002
1.0025=>1.002

意外!答えはJavaDocで。 

java.text
クラス DecimalFormat

丸め
DecimalFormat はフォーマットに直近への丸め (ROUND_HALF_EVEN を参照) を使用します。


 ROUND_HALF_EVEN

public static final int ROUND_HALF_EVEN

「もっとも近い数字」 に丸めるモードです。ただし、両隣りの数字が等距離の場合は偶数側に丸めます。破棄する小数部の左辺の桁が奇数の場合は ROUND_HALF_UP のように動作し、偶数の場合は ROUND_HALF_DOWN のように動作します。この丸めモードは、連続する計算で繰り返し適用される場合に累積エラーを最少にします。


Comments

6 Comments

Post a comment
  1. August 16, 2006

    ROUND_HALF_UP
    ROUND_HALF_DOWN
    ROUND_HALF_EVEN
    上の三つの区別がつかない

  2. April 17, 2013

    Hello there, I found your web site via Google at the same time
    as looking for a related topic, your site got
    here up, it seems to be good. I have bookmarked it in my google bookmarks.

    Hi there, simply was aware of your weblog via Google, and located
    that it is really informative. I am gonna be careful for brussels.
    I’ll appreciate for those who proceed this in future. A lot of folks might be benefited from your writing. Cheers!

  3. June 28, 2013

    I absolutely love your blog and find nearly all of your post’s to be precisely what I’m looking for.
    Do you offer guest writers to write content available for you?
    I wouldn’t mind producing a post or elaborating on a lot of the subjects you write in relation to here. Again, awesome site!

  4. July 3, 2013

    Quality content is the important to attract the users to
    visit the web site, that’s what this web site is providing.

  5. July 8, 2013

    Very soon this website will be famous among all blogging and site-building visitors,
    due to it’s pleasant articles or reviews

  6. March 10, 2014

    Undeniably imagine that that you said. Your favorite
    reason appeared to be at the internet the simplest thing to take into account
    of. I say to you, I definitely get irked at the
    same time as people think about concerns that they plainly do
    not realize about. You managed to hit the nail upon the highest as well as outlined out
    the entire thing with no need side-effects , folks can take a signal.
    Will probably be again to get more. Thanks

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Basic HTML is allowed. Your email address will not be published.

Subscribe to this comment feed via RSS