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

Join Date: Oct 2007
Posts: 3
Reputation: cutekola is an unknown quantity at this point 
Solved Threads: 0
cutekola's Avatar
cutekola cutekola is offline Offline
Newbie Poster

How to display the number of tens,ones and tenths in a given float value?

 
0
  #1
Oct 17th, 2007
  1. <span class="ad_notxt"><code class="inlinecode">// accept numbers from 0.1 to 99.9 then count the number of tens, ones, and tenths
  2.  
  3. import java.io.*;
  4.  
  5. public class accept99_9 {
  6.  
  7. public static void main(String[] args)throws IOException {
  8.  
  9. BufferedReader abc=new BufferedReader(new InputStreamReader(System.in));
  10. System.out.print("Enter a value: ");
  11. String input=abc.readLine();
  12. float v=Float.parseFloat(input);
  13. float tens=v %100;
  14. float ones=v %10;
  15. float tenths=v %10;
  16. System.out.println("Number of tens: "+tens);
  17. System.out.println("Number of ones: "+ones);
  18. System.out.println("Number of tenths: "+tenths);
  19. System.exit(0);
  20. }
  21. }
  22. </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........
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,415
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 256
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: How to display the number of tens,ones and tenths in a given float value?

 
0
  #2
Oct 17th, 2007
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.
  1. int tens=(int) ((v / 10) % 10);
  2. int ones=(int) (v % 10);
  3. 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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 3
Reputation: cutekola is an unknown quantity at this point 
Solved Threads: 0
cutekola's Avatar
cutekola cutekola is offline Offline
Newbie Poster

Re: How to display the number of tens,ones and tenths in a given float value?

 
0
  #3
Oct 17th, 2007
........wow........it worked......thanks a lot for your help sir masijade.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC