Recent

Author Topic: [SOLVED]Changing decimal places  (Read 934 times)

ortegahernandes

  • Newbie
  • Posts: 4
[SOLVED]Changing decimal places
« on: February 26, 2020, 12:46:14 am »
maybe here is a simple question ....
does anyone know how to invert numbers of two quantities. for example:
01> 10
02> 20
23> 32
45> 54
« Last Edit: February 26, 2020, 11:55:39 am by ortegahernandes »

winni

  • Hero Member
  • *****
  • Posts: 1145
Re: Changing decimal places
« Reply #1 on: February 26, 2020, 01:04:18 am »
Hi!

The simple solution is to convert the integers to strings,  read the string from the end and copy it mirrored to string2 and then comvert that to integer.

The second solution is that the difference of two mirrored integers is always a multiple of 9.

Winni

madref

  • Hero Member
  • *****
  • Posts: 772
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Changing decimal places
« Reply #2 on: February 26, 2020, 01:11:20 am »
It does sound like a schoolproject ::)
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 2.0.6 / FPC 3.0.4
Lazarus Trunc / FPC Trunc
Mac OS X Mojave

eljo

  • Full Member
  • ***
  • Posts: 181
Re: Changing decimal places
« Reply #3 on: February 26, 2020, 01:16:26 am »
the algorithm goes something like this

Repeat
  Digit := InNumber mod 10;
  Add Digit to a list.
  InNumber = inNumber div 10;
Until inNumber = 0
OutNumber := 0;
For Cntr := 0 to DigitList.Count -1 do
  outNumber := outNumber + DigitList(cntr) * (10^digitList.Count - Cntr);

Keep in mind that I haven't tested my assumptions and it probably has logical errors but its a start and avoids to and from string translations.
« Last Edit: February 26, 2020, 01:42:02 am by eljo »

winni

  • Hero Member
  • *****
  • Posts: 1145
Re: Changing decimal places
« Reply #4 on: February 26, 2020, 01:19:39 am »
It does sound like a schoolproject ::)

Hi!

Definitly it is.
And to convince the teacher that he is the next little Gauß, I would only take 11,22,33,44 ....

Winni

madref

  • Hero Member
  • *****
  • Posts: 772
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Changing decimal places
« Reply #5 on: February 26, 2020, 01:30:25 am »
 :D :D :D :D


Yes take the number 55,66,77,88,99 also

You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Lazarus 2.0.6 / FPC 3.0.4
Lazarus Trunc / FPC Trunc
Mac OS X Mojave

ortegahernandes

  • Newbie
  • Posts: 4
Re: Changing decimal places
« Reply #6 on: February 26, 2020, 01:58:54 am »
Thanks for the tips, especially the winni who gave the tip to separate and then add the characters inverted.
unfortunately it is not a school task, but for work.

I program a lot in C at work, but in Pascal I only take chances sometimes.

I did it like this, I found it very simple, because there are only two digits

var
temp_3 : string;
temp_4 : string; 
 
begin 
 temp_3 := copy(end_1.Caption,1,1);
 temp_4 := copy(end_1.Caption,2,2);
 end_5.Caption := (temp_4+temp_3);

end;
« Last Edit: February 26, 2020, 02:01:02 am by ortegahernandes »

Abelisto

  • Jr. Member
  • **
  • Posts: 90
Re: Changing decimal places
« Reply #7 on: February 26, 2020, 02:09:02 am »
y := x div 10 + (x mod 10) * 10;
OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk

winni

  • Hero Member
  • *****
  • Posts: 1145
Re: Changing decimal places
« Reply #8 on: February 26, 2020, 02:22:07 am »
Thanks for the tips, especially the winni who gave the tip to separate and then add the characters inverted.
unfortunately it is not a school task, but for work.

I program a lot in C at work, but in Pascal I only take chances sometimes.

I did it like this, I found it very simple, because there are only two digits

var
temp_3 : string;
temp_4 : string; 
 
begin 
 temp_3 := copy(end_1.Caption,1,1);
 temp_4 := copy(end_1.Caption,2,2);
 end_5.Caption := (temp_4+temp_3);

end;

Hi!

If it is only two digits then simplyfy it:

Code: Pascal  [Select]
  1. var source, dest : string:
  2. ...
  3. source := '10';
  4. setLength(dest,2);
  5. dest[1] := source[2];
  6. dest[2] := source[1];
  7.  
  8.  

Buena note
 Winni

Munair

  • Hero Member
  • *****
  • Posts: 514
  • keep it simple
    • SharpBASIC
Re: Changing decimal places
« Reply #9 on: February 26, 2020, 08:25:54 am »
y := x div 10 + (x mod 10) * 10;

This formula from Abelisto is probably the fastest solution for two-digit numbers. In function form:

Code: Pascal  [Select]
  1. function RevNum(const value: integer): integer;
  2. begin
  3.   result := value div 10 + (value mod 10) * 10;
  4. end;

Also works with negative values as long as -100 < value < 100. Even a value like -2 (-02) is converted correctly to -20.

While MOD and DIV are not the cheapest operations, they might still outperform string operations.
« Last Edit: February 26, 2020, 08:34:18 am by Munair »

eny

  • Hero Member
  • *****
  • Posts: 1589
Re: [SOLVED]Changing decimal places
« Reply #10 on: February 26, 2020, 12:54:40 pm »
Code: Pascal  [Select]
  1. procedure invert;
  2. const C_INV: array[0..99] of byte =
  3.         ( 00,10,20,30,40,50,60,70,80,90,01,11,21,31,41,51,61,71,81,91,
  4.           02,12,22,32,42,52,62,72,82,92,03,13,23,33,43,53,63,73,83,93,
  5.           04,14,24,34,44,54,64,74,84,94,05,15,25,35,45,55,65,75,85,95,
  6.           06,16,26,36,46,56,66,76,86,96,07,17,27,37,47,57,67,77,87,97,
  7.           08,18,28,38,48,58,68,78,88,98,09,19,29,39,49,59,69,79,89,99 );
  8. var i,j: integer;
  9. begin
  10.   i := 10;
  11.   j := C_INV[i];
  12.   // ...
  13. end;
:)         
All posts based on: Win10 (Win64); Lazarus 1.8.0 'stable' (#56594 win64) unless specified otherwise...

Abelisto

  • Jr. Member
  • **
  • Posts: 90
Re: [SOLVED]Changing decimal places
« Reply #11 on: February 26, 2020, 04:44:59 pm »
Code: Pascal  [Select]
  1. var
  2.     s: string[2];
  3. begin
  4.     readln(s);
  5.     word((@s[1])^) := swap(word((@s[1])^));
  6.     writeln(s);
  7. end.

For 2-characters strings
OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk

lucamar

  • Hero Member
  • *****
  • Posts: 2589
Re: [SOLVED]Changing decimal places
« Reply #12 on: February 26, 2020, 06:05:21 pm »
Abelisto,

First problem with your code: if you type a 1 character number it doesn't work correctly.

Correct like this:

Code: Pascal  [Select]
  1. var
  2.     s: string[2];
  3. begin
  4.     readln(s);
  5.     while Length(s) < 2 do s := '0' + s;
  6.     word((@s[1])^) := swap(word((@s[1])^));
  7.     writeln(s);
  8. end.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus 2.0.6/FPC 3.0.4 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Abelisto

  • Jr. Member
  • **
  • Posts: 90
Re: [SOLVED]Changing decimal places
« Reply #13 on: February 26, 2020, 06:17:49 pm »
Abelisto,

First problem with your code: if you type a 1 character number it doesn't work correctly.

Surely. It is simplified code for exactly two single-byte character values.
OS: Linux Mint + MATE, Compiler: FPC trunk (yes, I am risky!), IDE: Lazarus trunk

MaxCuriosus

  • Jr. Member
  • **
  • Posts: 66
Re: [SOLVED]Changing decimal places
« Reply #14 on: February 29, 2020, 03:54:16 pm »
I am late in the game but here is another solution:
Code: Pascal  [Select]
  1. Uses StrUtils
  2.  
  3. DoubleDigit:=ReverseString(DoubleDigit);