Count the number of digits after the decimal point in Java

I want to do

I want to count the number after the decimal point.

input(Float) output(int)
0.0 0
1.0 0
0.1 1
11.1 1
0.001 3


public int getPrecision(Float val){
  //In Java, the method of converting from Float to int is a lot of useless processing, so count it as a character string.
  String str = String.valueOf(val);

  //The end of the sentence".0"And".00000"All those ending with are 0 digits
    return 0;

  int index = str.indexOf(".");
  return str.substring(index + 1).length();


I can do what I want to do.

Below, the story I was addicted to making this

First, the regular expression part


When I wrote it as, the output became strange.

Input 0.0 Output 1 ← Wrong
Input 0.1 Output 1 ← Correct


When I debug it,


that? Does not match

Actually, this code was synonymous with the following code.



When using the String.matches method in java, use a regex that includes ^ and $

// \\.0+$When you want to use the regular expression of. .. ..


Recommended Posts

Count the number of digits after the decimal point in Java
Count the number of occurrences of a string in Ruby
[Note] Handling of Java decimal point
Number of digits in numeric item
The story of writing Java in Emacs
[Java] Check the number of occurrences of characters
Dynamically increase the number of elements in a Java 2D array (multidimensional array)
How to find the total number of pages when paging in Java
The story of low-level string comparison in Java
The story of making ordinary Othello in Java
About the idea of anonymous classes in Java
The story of learning Java in the first programming
Measure the size of a folder in Java
Feel the passage of time even in Java
I want to truncate after the decimal point
Import files of the same hierarchy in Java
[java] Decimal number → n-ary number n-ary number → decimal number
Get the URL of the HTTP redirect destination in Java
Set the maximum number of characters in UITextField in RxSwift
Change the storage quality of JPEG images in Java
Can the number of digits be Math.log10 (x) .floor + 1?
Summarize the additional elements of the Optional class in Java 9
Was done in the base year of the Java calendar week
[Ruby] Count an even number in an array using the even? Method
A quick explanation of the five types of static in Java
Get the next business day after the specified date in JAVA
Command to check the number and status of Java threads
A program that counts the number of words in a List
Implementation of gzip in java
How to derive the last day of the month in Java
About the default behavior of decimal point rounding using java.text.NumberFormat
[Java] Delete the specified number of characters from the end of StringBuilder
Implementation of tri-tree in Java
Find the number of days in a month with Kotlin
About the method to convert a character string to an integer / decimal number (cast data) in Java
Handling of java floating point [Note] while reading the reference book
Examine the system information of AWS Lambda operating environment in Java
[Java] Get the dates of the past Monday and Sunday in order
Output the difference between each field of two objects in Java
Find out the list of fonts available in AWS Lambda + Java
{The first consecutive 10-digit prime number in the value of e} .com
The milliseconds to set in /lib/ of Java jre is UTC
Examine the list of timezone IDs available in the Java ZoneId class
Get the public URL of a private Flickr file in Java
The point of addiction when performing basic authentication with Java URLConnection
Let's create a TODO application in Java 5 Switch the display of TODO
How to get the length of an audio file in java
How to increment the value of Map in one line in Java
Access the network interface in Java
[Java] Delete the elements of List
Guess the character code in Java
[Java version] The story of serialization
Specify the java location in eclipse.ini
List of members added in Java 9
Parsing the COTOHA API in Java
List of types added in Java 9
Order of processing in the program
The origin of Java lambda expressions
Implementation of like function in Java
Call the super method in Java
Set the number of seconds for fast forward and rewind in ExoPlayer