Statistics

Problem Statement for "NumberFormatter"

Problem Statement

You are a developer for a financial computer system. Users of the system often operate with large numbers, so it's important that numbers be formatted in an easy-to-read manner. You are given a String number containing the internal representation of a non-negative number. This number will contain only digits, along with an optional decimal separator represented by a comma (','). Format the number as follows:
1. If the number contains a decimal separator, replace the decimal separator with a period ('.').
2. Divide the integer part of the number into groups of three consecutive digits, from right to left. Depending on the number of digits, the left-most group might only contain 1 or 2 digits. Insert a single space (' ') between each pair of consecutive groups. (The integer part of the number is the part of the number to the left of the decimal separator if one exists, or the entire number if there is no decimal separator.)
For example, "1234567,890" would be formatted as "1 234 567.890" and "1024" would be formatted as "1 024". If insignificant leading or trailing zeroes exist in the number, they must be preserved. For example, "00003,1234000" would be formatted "00 003.1234000". Return the formatted version of the given number.

Definition

Class:
NumberFormatter
Method:
format
Parameters:
String
Returns:
String
Method signature:
String format(String number)
(be sure your method is public)

Constraints

  • number will contain between 1 and 50 characters, inclusive.
  • Each character in number will be either a digit ('0'-'9') or a comma (',').
  • number will contain at most one comma (',').
  • Comma (',') will not be the first or the last character of number.

Examples

  1. "1234567,890"

    Returns: "1 234 567.890"

    First example from the statement.

  2. "1024"

    Returns: "1 024"

    Second example from the problem statement.

  3. "00003,1234000"

    Returns: "00 003.1234000"

    Third example from the problem statement.

  4. "12345678912345678912345,0987654321"

    Returns: "12 345 678 912 345 678 912 345.0987654321"

  5. "100,000"

    Returns: "100.000"

  6. "000,000"

    Returns: "000.000"

  7. "0001"

    Returns: "0 001"

  8. "999"

    Returns: "999"

  9. "239"

    Returns: "239"

  10. "566,566"

    Returns: "566.566"

  11. "123,098"

    Returns: "123.098"

  12. "4792370981709,3718978979734"

    Returns: "4 792 370 981 709.3718978979734"

  13. "377,08923773908379730973963931273490734290732097"

    Returns: "377.08923773908379730973963931273490734290732097"

  14. "271744072109712974614088572"

    Returns: "271 744 072 109 712 974 614 088 572"

  15. "41"

    Returns: "41"

  16. "764523743716761471484822867"

    Returns: "764 523 743 716 761 471 484 822 867"

  17. "4269253168655853339351731"

    Returns: "4 269 253 168 655 853 339 351 731"

  18. "9368914947507381464569681638557495607997681396104"

    Returns: "9 368 914 947 507 381 464 569 681 638 557 495 607 997 681 396 104"

  19. "288236889003318394396335"

    Returns: "288 236 889 003 318 394 396 335"

  20. "4296612643678885433258025594089650167178154737905"

    Returns: "4 296 612 643 678 885 433 258 025 594 089 650 167 178 154 737 905"

  21. "181072738505512504786053778435445419915816160871"

    Returns: "181 072 738 505 512 504 786 053 778 435 445 419 915 816 160 871"

  22. "43230701386908175361642"

    Returns: "43 230 701 386 908 175 361 642"

  23. "9423742917543354662373059985629267120029288570"

    Returns: "9 423 742 917 543 354 662 373 059 985 629 267 120 029 288 570"

  24. "604785765803158427688567217589406014100039545"

    Returns: "604 785 765 803 158 427 688 567 217 589 406 014 100 039 545"

  25. "95511014015663868662"

    Returns: "95 511 014 015 663 868 662"

  26. "197455535409108834558300512033621561228176889"

    Returns: "197 455 535 409 108 834 558 300 512 033 621 561 228 176 889"

  27. "6174216133054526470"

    Returns: "6 174 216 133 054 526 470"

  28. "1002914839024010448"

    Returns: "1 002 914 839 024 010 448"

  29. "6102685628077558574698040511965821664232992"

    Returns: "6 102 685 628 077 558 574 698 040 511 965 821 664 232 992"

  30. "962502957050225006"

    Returns: "962 502 957 050 225 006"

  31. "6239044102090580,78505324234406217825203"

    Returns: "6 239 044 102 090 580.78505324234406217825203"

  32. "49251373419813519439920838,83313006317755389"

    Returns: "49 251 373 419 813 519 439 920 838.83313006317755389"

  33. "512567442431,81468767251392444207567623616745"

    Returns: "512 567 442 431.81468767251392444207567623616745"

  34. "0005111406744488431353033181443631727077978257,842"

    Returns: "0 005 111 406 744 488 431 353 033 181 443 631 727 077 978 257.842"

  35. "91119765245987878218486424723779,42281599708"

    Returns: "91 119 765 245 987 878 218 486 424 723 779.42281599708"

  36. "031131350691525057,52907253848862927361"

    Returns: "031 131 350 691 525 057.52907253848862927361"

  37. "24128,068844181332021050369558939"

    Returns: "24 128.068844181332021050369558939"

  38. "019895381823368,6856962513283143881"

    Returns: "019 895 381 823 368.6856962513283143881"

  39. "1,9849080076914833937"

    Returns: "1.9849080076914833937"

  40. "259883798219671218305811253152672811,67618544"

    Returns: "259 883 798 219 671 218 305 811 253 152 672 811.67618544"

  41. "538469299094001057365,72183345994"

    Returns: "538 469 299 094 001 057 365.72183345994"

  42. "6584139,8"

    Returns: "6 584 139.8"

  43. "2894338843746685,072235"

    Returns: "2 894 338 843 746 685.072235"

  44. "650813888860894043020861449,937841"

    Returns: "650 813 888 860 894 043 020 861 449.937841"

  45. "4294222018509712695695472025762944594,6"

    Returns: "4 294 222 018 509 712 695 695 472 025 762 944 594.6"

  46. "54846690909262754493759,3"

    Returns: "54 846 690 909 262 754 493 759.3"

  47. "82805240,87705"

    Returns: "82 805 240.87705"

  48. "0"

    Returns: "0"

  49. "1"

    Returns: "1"

  50. "0,0"

    Returns: "0.0"

  51. "9,0"

    Returns: "9.0"

  52. "0,1"

    Returns: "0.1"

  53. "5,5"

    Returns: "5.5"

  54. "989898989898989,0000"

    Returns: "989 898 989 898 989.0000"

  55. "111111,0"

    Returns: "111 111.0"

  56. "123"

    Returns: "123"

  57. "123456,123456"

    Returns: "123 456.123456"

  58. "111"

    Returns: "111"

  59. "777"

    Returns: "777"


This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2024, TopCoder, Inc. All rights reserved.
This problem was used for: