# Convert a JAVA floating point to a percentage to separate the integer from the decimal part.

Source: Internet
Author: User

Convert a JAVA floating point to a percentage to separate the integer from the decimal part.
JAVA floating point number to percentage

Public class DoubleToPercentformat {

/**
* Convert double type data to percent format, and retain the IntegerDigits before the decimal point and FractionDigits after the 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); // number of digits before the decimal point
Nf. setMinimumFractionDigits (FractionDigits); // number of digits 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; intln (the integer part of num + = + I + decimal part is + f );}}`

`First, the upstairs method is not an object-oriented idea, and it cannot be used in practice at all. It needs to be modified if used. Second, after intercepting the decimal part in the upstairs method, there will be several more digits in the decimal part. For example, after intercepting the decimal part of 123.321, 0.32100055 is displayed. My method completely overcomes the disadvantages of the upstairs method. I first wrote a variety of ApartFloat types to separate float floating point numbers. When you want to separate floating point numbers, you only need to call two static methods in this class. The returnIntegralPart (float n) method is used to return the integer part of the floating point to be separated; The returnDecimalPart (float n) method is used to return the fractional part of the floating point to be separated. ApartFloat class source code: // This class is used to break down the floating point public class ApartFloat {// extract the integer part of the floating point. static int returnIntegralPart (float n) {int I = (int) n; return I ;} // extract the decimal part of the floating point number // when the decimal part is extracted, I convert the number into a string and then intercept it. // in this way, other digits are not added after the truncated decimal point. static string returnDecimalPart (float n) {Float m = new Float (n); String I = String (); int a = dexOf (.); I = bstring (a + 1); return I;} the user can call these two methods in any class anywhere to separate floating point numbers. For example, call public class Aaa {public static void main (String [] args) {intln (I need an integer part of floating point number 123.321: + turnIntegralPart (123.321f) as follows )); intln (I need a floating point number with a decimal part of 123.321: + turnDecimalPart (123.321f);} you only need to fill in the brackets for separation, but be sure to use the float type.`

Related Keywords:

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.