[JAVA] Divide the character string starting from the decimal point (using index.Of, substring)

background

When I wanted to change the calculation method before and after the decimal point, I stumbled a little and solved it, so make a note of it.

environment

Eclipse Neon 4.6.3 Java8

Class used

-Whether the character string contains a decimal point int variable name = string variable .indexOf ("character you want to look up");

-Getting the character string before and after the decimal point String variable .substring (x, y); x = position of the first character y = end character position (optional)

I tried using it

public class strPractice {

    public static void main(String[] args) {        
        
        String a = "12345.6789";
        
        //In a string"."Is included
        //The location if included(Starting from 0)If not-Returns 1
        int b = a.indexOf(".");
        System.out.println((b + 1) + "It is included in the second.");
        
        //Divide the searched character as the starting point
        //first half
        String c = a.substring(0 , b);
        System.out.println("Characters to the decimal point" + c + "is.");
               
        //Latter half
        String d = a.substring(b + 1);
        System.out.println("The characters after the decimal point are" + d + "is.");

    }

}

Execution result

It is included in the 6th.
The character to the decimal point is 12345.
The character after the decimal point is 6789.

Summary

I used this method to convert a decimal number received in form to a binary number (because the decimal point calculation is special for binary numbers). I thought that I would normally use it as an address.

Recommended Posts

Divide the character string starting from the decimal point (using index.Of, substring)
Deletes after the specified character from the character string
Set the date and time from the character string with POI
Change only one character from the String type string to uppercase
About the default behavior of decimal point rounding using java.text.NumberFormat
I want to find out what character the character string appears from the left