JAVA floating-point number converted to percent, separating integers and fractional parts

Source: Internet
Author: User

JAVA floating-point numbers converted to percentages

public class Doubletopercentformat {


/**
* Convert double type data to percent format and preserve integerdigits and fractiondigits digits before decimal point
* @param D
* @param integerdigits
* @param fractiondigits
* @return
*/
public static String Getpercentformat (double d,int integerdigits,int fractiondigits) {
NumberFormat NF = java.text.NumberFormat.getPercentInstance ();
Nf.setmaximumintegerdigits (integerdigits);//keep several before the decimal point
Nf.setminimumfractiondigits (fractiondigits);//Keep several after the decimal point
String str = Nf.format (d);
return str;
}

}

public class convert{public  static void Main (String args[]) {  float  num=28.8f;  int i= (int) num;  float  f=num-i; integral part of intln (num+ "=" +i+ "\ T fractional part" +f);}}

The first, the upstairs method is not the object-oriented thought, simply can not be used in practice, and the words need to be modified. Second, the method of the upper part of the block after the fractional section, the following number of small chapters more than a few numbers. For example, after capturing 123.321 of the decimal point, it will show 0.32100055. My method completely overcomes the shortcomings of the upstairs method. I first write a variety of apartfloat for separating float floating-point numbers. When a user wants to detach a floating-point number, only the two static methods inside the class can be called. Where the Returnintegralpart (float N) method is used to return the integer portion of the floating-point number to be detached; the Returndecimalpart (float N) method returns the fractional part of the floating-point numbers to be detached. Apartfloat class Source code://This class is used to decompose floating-point number public class apartfloat{//extract floating-point integer part of the static int Returnintegralpart (float n) {int i= (int) n; return i;} Extract the fractional portion of a floating-point number//extract the decimal part when I convert the number to a string and then intercept//Do not appear after the truncated decimal number to increase the phenomenon of other numbers static String Returndecimalpart (float n) {float m=new Float (n); String i= string (); int a= dexof ("."); i= bstring (a+1); return i;}} Users can call these two methods in any class anywhere to separate floating-point numbers. For example, call as follows: public class Aaa{public static void Main (string[] args) {intln ("I need an integral part of a floating-point number 123.321:" + turnintegralpart ( 123.321f)); INTLN ("I need a fractional part of a floating-point number 123.321:" + turndecimalpart (123.321f));}} If you want to separate who can be in parentheses, but attention must be float type.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

JAVA floating-point number converted to percent, separating integers and fractional parts

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.