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

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

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

Disclosure: My content contains affiliate links.

43,082 questions

55,955 answers

573 users

How to replace all occurrences of a specific digit in a floating-point number with Pascal

1 Answer

0 votes
program ReplaceDigitsInReal;

function ReplaceAllDigits(num: Real; oldDigit, newDigit: Char): Real;
var
  strNum: string[20];
  i: Integer;
  code: Integer;
  modifiedNum: Real;
begin
  Str(num:0:3, strNum);  { Convert real to string with 3 decimal places }

  for i := 1 to Length(strNum) do
    if strNum[i] = oldDigit then
      strNum[i] := newDigit;

  Val(strNum, modifiedNum, code);  { Convert string back to real }
  if code = 0 then
    ReplaceAllDigits := modifiedNum
  else
    ReplaceAllDigits := 0.0;  { Fallback in case of conversion error }
end;

begin
  Writeln(ReplaceAllDigits(82420.291, '2', '7'):0:3);
  Writeln(ReplaceAllDigits(111.11, '1', '5'):0:3);
end.



(*
run:

87470.791
555.550

*)


 



answered Nov 18, 2025 by avibootz

Related questions

...