Count digits after decimal including last zero

count number of digits after decimal point in java
count number of digits after decimal point in c++
number of digits after decimal point c++
count number of digits after decimal point in python
count number of digits after decimal point in c#
how to count digits after decimal point
get number of digits after decimal point
count number of digits after decimal on dividing a number

I have this,

import java.util.Scanner;

public class StringDecimalPartLength {

    public static void main(String[] args){
       Scanner input = new Scanner(System.in);
       System.out.print("Enter a decimal number: ");
       Double string_Temp = Double.parseDouble(input.nextLine().replace(',', '.'));
       String string_temp = Double.toString(string_Temp);
       String[] result = string_temp.split("\\.");
       System.out.print(result[1].length() + " decimal place(s)");
    }
}

it works until I enter a number with trailing zero, such as 4,90. It ignores the zero and returns 1.

How to fix this? Thank you!

Since you are already reading the input as a string, you can save that value and then test to see if it is a valid decimal number:

import java.util.Scanner;

public class StringDecimalPartLength {
    public static void main(String[] args){
        Scanner input = new Scanner(System.in);
        System.out.print("Enter a decimal number: ");
        String value = input.nextLine().replace(',', '.');

        try {
            Double.parseDouble(value);

            String[] result = value.split("\\.");

            System.out.print(result[1].length() + " decimal place(s)");
        } catch (NumberFormatException e) {
            System.out.println("The entered value is not a decimal number.");
        }
    }
}

Count number of digits after decimal on dividing a number , CPP program to count digits after dot when a. // number is divided by another. # include <bits/stdc++.h>. using namespace std;. int count( int x, int y). {. int ans = 0 � Re: count of numbers after decimal point with trailing zeros. =len (left (a1,find (" ",a1)-1))-find (".",a1) "Unless otherwise stated all my comments are directed at OP". Mojito connoisseur and now happily retired.

4.9 or 4.90 as a double are both represented by the same approximation. There is no difference between them.

You can, of course, do what you want using String processing on the input. However, you may find BigDecimal a better data type for what you are doing. It distinguishes between 4.9 and 4.90, and can represent each of them exactly.

import java.math.BigDecimal;

public class Test {
  public static void main(String[] args) {
    System.out.println(new BigDecimal(4.9));
    System.out.println(new BigDecimal(4.90));
    BigDecimal x1 = new BigDecimal("4.9");
    System.out.println(x1);
    System.out.println(x1.scale());
    BigDecimal x2 = new BigDecimal("4.90");
    System.out.println(x2);
    System.out.println(x2.scale());
  }
}

Output:

4.9000000000000003552713678800500929355621337890625
4.9000000000000003552713678800500929355621337890625
4.9
1
4.90
2

Accuracy and Scientific Notation, If a number has a decimal point, the significant digits are the digits starting from the first non-zero number on the left to the last digit written at the right end. In either case the number of significant digits is just the count of these digits. If you are interested only in significant digits, then before Decimal: =FIND(".",A1)-1 after Decimal: =LEN(A1)-FIND(".",A1) But if you are interested in also counting trailing or leading zeros, you will either need to enter the numbers as text, or use VBA to determine the format.--ron

Omari, I adjusted your code like this and it worked, thanks!

if(result[0].length() == 0 ){

            System.out.print("The entered value is not a decimal number.");
        }
        else {
            System.out.print(result[1].length() + " decimal place(s)");
        }

    } catch (NumberFormatException e) {

        System.out.println("The entered value is not a decimal number.");

    }catch (ArrayIndexOutOfBoundsException e) {
System.out.print("Error Message" );     
      }

A Brief on Significant Figures, 2) Decimal Numbers The three zeros before 235 but after the decimal point do not count as significant. For a decimal number the very last zeros count as significant. You may use a more precise tool and come up with L = 7.602 cm. Zeros can be used as (insignificant) place holders to the left of significant digits if the number is a decimal. For example, a mass of 42 g has two significant digits. Expressed in kilograms, the mass of 0.042kg should still have two significant digits. The zeros to the left of the 4 are place holders and not significant:

Measurement Uncertainty, The trailing zeros do not count as significant. Trailing zeros in a number containing a decimal point are significant. Therefore, any zeros after the decimal point are also significant. Example: 0.00 has three significant figures. Example: 88,485.90 ----- The result is 5.9 (But I need here.90 inculding last ZERO) 88,485.95----- The result is .95 (Here it is okay as it gets last 3 digits after Decimal)

Place Value: Decimal Numbers, Explains how decimal places are derived, and demonstrates how to work with their to the right counts pieces that are 1/10 as big as the piece at the previous step, If we didn't include that zero between the dot and the 7, we'd be saying "7/ 10", at a decimal-places number, we count up the digits after the decimal point. Zeros appearing in front of nonzero digits are not significant: 0.095 987 m has five sig figs; 0.0009 kg has one sig fig. Zeros at the end of a number and to the right of a decimal are significant: 85.00 g has four sig figs; 9.000 000 000 mm has 10 sig figs. Zeros at the end of a number but to the left of a decimal may or may not be significant.

how to calculate digits after decimal point?, Learn more about decimal, trailing zeros, digits after decimal, audio, after decimal and then multiply the original number with this count in� Count the number of decimal places in the two factors, add these two numbers together (3 + 2 = 5), and place the decimal point in the answer so that it has five digits after the decimal point. After you place the decimal point (but not before!), you can drop the trailing zero. 0.675 x 34.8 = 23.49.

Comments
  • Have you calculating the length before converting to a double? If you convert "4.90" into a double, you get 4.9. Then if you convert back to string you get "4.9".
  • thanks guys, I now get the logic behind it
  • thanks Omari, it worked, yet it didn't catch all the errors. I had to adjust it and it worked.
  • when something like 1. was entered, it didn't catch the exception, now it does
  • @RoiboisTx That would be because in parsing the value 1 can be converted to a decimal number since it is equivalent to 1.0.
  • I will look into this approach, appreciate it.