Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert integer whole value to 3 digit dot seperated string

I can't believe I am struggling so much with this! Hopefully it is an easy one. Using either Delphi or Freepascal:

Given the whole integer value "1230", or "1850", how do you format that as a floating point string of 3 digits where the decimal is in the 3rd position, and the trailing digit discarded.

Example

1230 means "v12.3" 1850 means "v18.5"

So I need to convert the first two digits to a string. Then insert a decimal place. Convert the third digit to a string after the decimal place. And discard the zero. I've looked at Format, FormatFloat, Format, and several others, and they all seem to equate to taking existing floating point numbers to strings, or floating point strings to numbers.

like image 671
Gizmo_the_Great Avatar asked Nov 26 '19 13:11

Gizmo_the_Great


People also ask

How do you convert a number to a comma separated string?

To parse a string with commas to a number: Use the replace() method to remove all the commas from the string. The replace method will return a new string containing no commas.

How can I parse a string with a comma thousand separator to a number?

We can parse a number string with commas thousand separators into a number by removing the commas, and then use the + operator to do the conversion. We call replace with /,/g to match all commas and replace them all with empty strings.

How do you treat integers as strings?

In Python an integer can be converted into a string using the built-in str() function. The str() function takes in any python data type and converts it into a string.

How to convert a number to a 3 digit string?

If you're just formatting a number, you can just provide the proper custom numeric format to make it a 3 digit string directly: @coeing realize that this will only work when number is an int. If number is a double, for instance, it will have to be string.Format (" {0:000}", number) Is there a scenario where that would yield a different result?

How do I convert an integer to a string in JavaScript?

To convert the integer 12 to a string value, you can pass 12 into the str () method: The quotes around the number 12 signify that the number is no longer an integer but is now a string value.

How to convert a number to a string in Java?

But if your variable is already an instance of Integer (wrapper class of the primitive type int), it is better to just invoke its toString () method as shown above. This method is not efficient as an instance of Integer class is created before conversion is performed. The class java.text.DecimalFormat is a class that formats a number to a String.

How to convert integers to strings in Python?

To use positional formatting, use the "%s" % prefix in front of the number, and Python will do the rest. 4. Use of __str__ () Method Like the str () function, there is a __str__ () method in Python, which is a close alternative for converting integers to strings.


3 Answers

Just assign your integer value to a float and divide by 100.0.

Use Format() or FormatFloat() to convert the value to a string with three digits and a decimal point:

program Project8;

{$APPTYPE CONSOLE}

uses
  System.SysUtils;

var
  i : Integer;
const
  myFormat : TFormatSettings = (DecimalSeparator: '.');
begin
  i := 1230;
  WriteLn(Format('v%4.1f',[i/100.0],myFormat));   // Writes v12.3
  WriteLn(FormatFloat('v##.#',i/100.0,myFormat)); // Writes v12.3
  ReadLn;
end.
like image 144
LU RD Avatar answered Nov 15 '22 09:11

LU RD


I personally don't much care for using floating point arithmetic (the divide by 10 approach seen in other answers) when integer arithmetic can do the job. You are converting the number to an imperfect representation and then rounding to one decimal place. These solutions will work, because you can put a sufficiently tght bound on the representation inaccuracy. But why even fire up the floating point unit when the arithmetic can be done exactly using integer operations?

So I would always opt for something on these lines.

Major := Version div 100;
Minor := (Version mod 100) div 10;

Where Version is your input value. This can then be converted into a string like so:

VersionText := Format('%d.%d', [Major, Minor]);

You can even do the conversion without any explicit arithmetic:

VersionText := IntToStr(Version);
N := Length(VersionText);
VersionText[N] := VersionText[N-1];
VersionText[N-1] := '.';
like image 23
David Heffernan Avatar answered Nov 15 '22 10:11

David Heffernan


I am not sure if there is a cleaner or better way, but my suggestion would be to use copy in conjunction with IntToStr.

var
  number,version:string;
begin
  number = IntToStr(1850);
  version = 'V'+copy(number,1,2)+'.'+copy(number,3,1);
end;

You are free to add checks of the length of the number string, depending on your input values.

like image 22
Ancaron Avatar answered Nov 15 '22 09:11

Ancaron