Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within Beginning Java
Search Coderanch
Advance search
Google search
Register / Login
Help coderanch get a
new server
by contributing to the
fundraiser
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Ron McLeod
Paul Clapham
Devaka Cooray
Liutauras Vilda
Sheriffs:
Jeanne Boyarsky
paul wheaton
Henry Wong
Saloon Keepers:
Stephan van Hulst
Tim Holloway
Tim Moores
Carey Brown
Mikalai Zaikin
Bartenders:
Lou Hamers
Piet Souris
Frits Walraven
Forum:
Beginning Java
How to find number of decimal places?
Ajith George
Ranch Hand
Posts: 109
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
How can i find number of decimal places in a double value.
example number of decimal places in 0.0005.
Thanks in advance.
SCJP 1.4, Brainbench
LinkedIn
-
Blog
Keith Lynn
Ranch Hand
Posts: 2412
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You could convert it to a
String
, find the index of the . and then find the difference between that index and the length of the String.
Ajith George
Ranch Hand
Posts: 109
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
But the double number passed in to the method is automatically converted in to the format 5.0E-6 for .000005 . So that idea is not working. Any other suggstions, please help.
SCJP 1.4, Brainbench
LinkedIn
-
Blog
Keith Lynn
Ranch Hand
Posts: 2412
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Well if it's in scientific notation,
you should
be able to use the exponent to figure out the number of decimal places.
Ajith George
Ranch Hand
Posts: 109
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
But how can i use this exponebt number. itried the following code
char theChar = strNumber.charAt(stringLength-1); numberOfDecimals=(int)theChar;
But some garbage value too is coming with variable theChar.
What should i do?
SCJP 1.4, Brainbench
LinkedIn
-
Blog
Keith Lynn
Ranch Hand
Posts: 2412
posted 18 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Here is a way to start if you have the number in scientific notation.
import java.math.*; public class Test14 { public static void main(String[] args) { double d = 5.0E-6; String string = String.valueOf(d); String[] tokens = string.split("[+-]"); int decimalPlaces = 0; if (string.contains("-")) decimalPlaces = new Integer(tokens[1]); System.out.println(decimalPlaces); } }
Consider Paul's
rocket mass heater
.
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Urgent A method in Java which rounds 18 places to 8 places after decimal
Excel display integer instead of double
Dividing Ints
Problem with DecimalFormat
floating point round to 2.
More...