| | |
How to display the number of tens,ones and tenths in a given float value?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
Java Syntax (Toggle Plain Text)
<span class="ad_notxt"><code class="inlinecode">// accept numbers from 0.1 to 99.9 then count the number of tens, ones, and tenths import java.io.*; public class accept99_9 { public static void main(String[] args)throws IOException { BufferedReader abc=new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter a value: "); String input=abc.readLine(); float v=Float.parseFloat(input); float tens=v %100; float ones=v %10; float tenths=v %10; System.out.println("Number of tens: "+tens); System.out.println("Number of ones: "+ones); System.out.println("Number of tenths: "+tenths); System.exit(0); } } </code></span>
How will i come up with an output like these?
example....
Enter a value: 12.3
Number of tens: 1
Number of ones: 2
Number of tenths: 3
thanks........
you're looking for ints, and the last one must be mutiplied by 10 other wise the int value will always be 0 (obviously) and the first one divided by ten or it will be e.g. 10 instead of 1.
Java Syntax (Toggle Plain Text)
int tens=(int) ((v / 10) % 10); int ones=(int) (v % 10); int tenths=(int) ((v * 10) % 10);
Last edited by masijade; Oct 17th, 2007 at 5:31 pm. Reason: forgot to divide the first one by ten
Java Programmer and Sun Systems Administrator
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
----------------------------------------------
Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
![]() |
Similar Threads
- number of times each number in array occurs? (C)
- display the number of capitals letters in the sentence (Assembly)
Other Threads in the Java Forum
- Previous Thread: Getting Modified TimeStamp
- Next Thread: cannot find symbol error message
| Thread Tools | Search this Thread |
-xlint add android api applet application array arrays automation bi binary blackberry bluetooth chat class classes client code compile compiler component converter database digit eclipse equation error event exception fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui health html hyper ide idea image input int integer j2me java javame javaprojects jetbrains jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie nonstatic notdisplaying page pearl print problem program programming project qt recursion scanner screen scrollbar server set size sms sort spamblocker sql string superclass swing system thread threads time tree variablebinding windows xor






