code_junkie

Extracting individual digits from a float

Nov 12th, 2011
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.40 KB | None | 0 0
  1. char string[99];
  2. sprintf(string,"%f",floatValue);
  3.  
  4. #define SHOW(X) cout << # X " = " << (X) << endl
  5.  
  6. int
  7. main()
  8. {
  9. double d = 1234.567;
  10.  
  11. SHOW( (int(d)%10000) / 1000 );
  12. SHOW( (int(d)%1000) / 100 );
  13. SHOW( (int(d)%100) / 10 );
  14. SHOW( (int(d)%10) );
  15. SHOW( (int(d*10) % 10) );
  16. SHOW( (int(d*100) % 10) );
  17. SHOW( (int(d*1000)% 10) );
  18.  
  19. SHOW( log(d)/log(10) );
  20. }
Add Comment
Please, Sign In to add comment