Advertisement
Guest User

Untitled

a guest
Sep 4th, 2017
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.61 KB | None | 0 0
  1. import java.io.*;
  2. import java.math.BigDecimal;
  3. import java.math.RoundingMode;
  4. import java.text.DecimalFormat;
  5. import java.util.Scanner;
  6.  
  7. public class Kek
  8. {
  9. public static void main(String args[]) throws IOException
  10. {
  11. Scanner in = new Scanner(new FileInputStream("in"));
  12. StringBuilder r = new StringBuilder();
  13. BigDecimal num;
  14. while(true)
  15. {
  16. try
  17. {
  18. String s = in.nextLine();
  19. if (s.equals("#"))
  20. {
  21. System.out.println(r);
  22. return;
  23. }
  24. int ind = s.indexOf('.');
  25. if (ind == s.length() - 1 || Character.toLowerCase(s.charAt(ind + 1)) == 'e')
  26. throw new NumberFormatException();
  27. num = new BigDecimal(s);
  28. }
  29. catch (NumberFormatException e)
  30. {
  31. r.append("Not a floating point number\n");
  32. in.nextLine();
  33. continue;
  34. }
  35. int n = Integer.parseInt(in.nextLine());
  36. DecimalFormat f = new DecimalFormat("#.#");
  37. f.setRoundingMode(RoundingMode.DOWN);
  38. f.setMinimumFractionDigits(n);
  39. f.setMaximumFractionDigits(n);
  40. StringBuilder out = new StringBuilder(f.format(num));
  41. if (out.toString().equals("-0,0"))
  42. out = new StringBuilder("0,0");
  43. int ind = out.indexOf(",");
  44. if (ind != -1)
  45. out.setCharAt(ind, '.');
  46. r.append(out + "\n");
  47. }
  48. }
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement