Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to format a number with thousands separator (commas) in Pascal

1 Answer

0 votes
program FormatNumberWithCommas;

uses
  SysUtils; // IntToStr

function AddThousandsSeparator(number: Int64): string;
var
  numStr: string;
  i, j, len: Integer;
  resultStr: string;
begin
  // Convert the number to a string
  numStr := IntToStr(number);
  len := Length(numStr);
  resultStr := '';

  // Start inserting characters with commas
  j := 0; // Keeps track of how many digits have been added since the last comma
  for i := len downto 1 do
  begin
    resultStr := numStr[i] + resultStr; // Append digit at current position

    // Add a comma after every 3 digits (except for the first group)
    Inc(j);
    if (j mod 3 = 0) and (i > 1) then
      resultStr := ',' + resultStr;
  end;

  // Return the formatted result
  AddThousandsSeparator := resultStr;
end;

var
  number: Int64;
begin
  number := 9334567890128;

  WriteLn('Formatted number with commas: ', AddThousandsSeparator(number));
end.



(*
run:

Formatted number with commas: 9,334,567,890,128

*)

 



answered Mar 30, 2025 by avibootz

Related questions

1 answer 152 views
1 answer 104 views
1 answer 97 views
1 answer 109 views
2 answers 177 views
1 answer 163 views
...