Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
6
Task/Formatted-numeric-output/00-META.yaml
Normal file
6
Task/Formatted-numeric-output/00-META.yaml
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
---
|
||||
category:
|
||||
- Text processing
|
||||
- Simple
|
||||
from: http://rosettacode.org/wiki/Formatted_numeric_output
|
||||
note: Basic language learning
|
||||
7
Task/Formatted-numeric-output/00-TASK.txt
Normal file
7
Task/Formatted-numeric-output/00-TASK.txt
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
;Task:
|
||||
Express a number in decimal as a fixed-length string with leading zeros.
|
||||
|
||||
|
||||
For example, the number '''7.125''' could be expressed as '''00007.125'''.
|
||||
<br><br>
|
||||
|
||||
|
|
@ -0,0 +1 @@
|
|||
print(‘#05.3’.format(7.125))
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
.model small
|
||||
.stack 1024
|
||||
.data
|
||||
;data segment is unused in this program
|
||||
.code
|
||||
|
||||
start:
|
||||
|
||||
mov ax,@code
|
||||
mov ds,ax
|
||||
mov es,ax
|
||||
|
||||
cld ;make lodsb, etc. auto-increment
|
||||
|
||||
mov al, byte ptr [ds:LeadingZeroes]
|
||||
mov cl,al
|
||||
mov ch,0
|
||||
mov al,'0' ;30h
|
||||
jcxz DonePrintingLeadingZeroes ;there are leading zeroes so we won't skip that section. This branch is not taken.
|
||||
|
||||
printLeadingZeroes:
|
||||
call PrintChar ;print ascii 0 to the terminal 4 times
|
||||
loop printLeadingZeroes
|
||||
|
||||
DonePrintingLeadingZeroes:
|
||||
|
||||
mov si, offset TestString
|
||||
call PrintString
|
||||
|
||||
mov al, byte ptr [ds:TrailingZeroes]
|
||||
mov cl,al
|
||||
mov ch,0
|
||||
mov al,'0' ;30h
|
||||
jcxz DonePrintingTrailingZeroes ;there are none in this example so this branch is always taken
|
||||
printTrailingZeroes:
|
||||
call PrintChar
|
||||
loop printTrailingZeroes
|
||||
|
||||
DonePrintingTrailingZeroes:
|
||||
mov ax,4C00h
|
||||
int 21h ;exit to DOS
|
||||
|
||||
TestString byte "7.125",0
|
||||
|
||||
LeadingZeroes byte 4 ;number of leading zeroes to print
|
||||
TrailingZeroes byte 0 ;number of trailing zeroes to print
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
7.125 "%09.3f" s:strfmt
|
||||
. cr
|
||||
|
|
@ -0,0 +1,46 @@
|
|||
/* ARM assembly AARCH64 Raspberry PI 3B */
|
||||
/* program formatNum64.s */
|
||||
/* use C library printf ha, ha, ha !!! */
|
||||
|
||||
/*******************************************/
|
||||
/* Constantes file */
|
||||
/*******************************************/
|
||||
/* for this file see task include a file in language AArch64 assembly*/
|
||||
.include "../includeConstantesARM64.inc"
|
||||
/*******************************************/
|
||||
/* Initialized data */
|
||||
/*******************************************/
|
||||
.data
|
||||
szFormat1: .asciz " %09.3f\n"
|
||||
.align 4
|
||||
sfNumber: .double 0f-7125E-3
|
||||
sfNumber1: .double 0f7125E-3
|
||||
/*******************************************/
|
||||
/* UnInitialized data */
|
||||
/*******************************************/
|
||||
.bss
|
||||
.align 4
|
||||
/*******************************************/
|
||||
/* code section */
|
||||
/*******************************************/
|
||||
.text
|
||||
.global main
|
||||
main: // entry of program
|
||||
|
||||
ldr x0,qAdrszFormat1 // format
|
||||
ldr x1,qAdrsfNumber // float number address
|
||||
ldr d0,[x1] // load float number in d0
|
||||
bl printf // call C function !!!
|
||||
ldr x0,qAdrszFormat1
|
||||
ldr x1,qAdrsfNumber1
|
||||
ldr d0,[x1]
|
||||
bl printf
|
||||
|
||||
100: // standard end of the program
|
||||
mov x0,0 // return code
|
||||
mov x8,EXIT // request to exit program
|
||||
svc 0 // perform the system call
|
||||
|
||||
qAdrszFormat1: .quad szFormat1
|
||||
qAdrsfNumber: .quad sfNumber
|
||||
qAdrsfNumber1: .quad sfNumber1
|
||||
|
|
@ -0,0 +1,17 @@
|
|||
main:(
|
||||
REAL r=exp(pi)-pi;
|
||||
print((r,newline));
|
||||
printf(($g(-16,4)l$,-r));
|
||||
printf(($g(-16,4)l$,r));
|
||||
printf(($g( 16,4)l$,r));
|
||||
printf(($g( 16,4,1)l$,r));
|
||||
printf(($-dddd.ddddl$,-r));
|
||||
printf(($-dddd.ddddl$,r));
|
||||
printf(($+dddd.ddddl$,r));
|
||||
printf(($ddddd.ddddl$,r));
|
||||
printf(($zzzzd.ddddl$,r));
|
||||
printf(($zzzz-d.ddddl$,r));
|
||||
printf(($zzzz-d.ddddedl$,r));
|
||||
printf(($zzzz-d.ddddeddl$,r));
|
||||
printf(($4z-d.4de4dl$,r))
|
||||
)
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
'ZF15.9' ⎕FMT 7.125
|
||||
00007.125000000
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
/* ARM assembly Raspberry PI */
|
||||
/* program formatNum.s */
|
||||
/* use C library printf ha, ha, ha !!! */
|
||||
/* Constantes */
|
||||
.equ EXIT, 1 @ Linux syscall
|
||||
/* Initialized data */
|
||||
.data
|
||||
szFormat1: .asciz " %09.3f\n"
|
||||
.align 4
|
||||
sfNumber: .double 0f-7125E-3
|
||||
sfNumber1: .double 0f7125E-3
|
||||
|
||||
/* UnInitialized data */
|
||||
.bss
|
||||
.align 4
|
||||
|
||||
/* code section */
|
||||
.text
|
||||
.global main
|
||||
main: @ entry of program
|
||||
push {fp,lr} @ saves registers
|
||||
|
||||
ldr r0,iAdrszFormat1 @ format
|
||||
ldr r1,iAdrsfNumber @ number address
|
||||
ldr r2,[r1] @ load first 4 bytes
|
||||
ldr r3,[r1,#4] @ load last 4 bytes
|
||||
bl printf @ call C function !!!
|
||||
ldr r0,iAdrszFormat1
|
||||
ldr r1,iAdrsfNumber1
|
||||
ldr r2,[r1]
|
||||
ldr r3,[r1,#4]
|
||||
bl printf
|
||||
|
||||
|
||||
|
||||
100: @ standard end of the program
|
||||
mov r0, #0 @ return code
|
||||
pop {fp,lr} @restaur registers
|
||||
mov r7, #EXIT @ request to exit program
|
||||
swi 0 @ perform the system call
|
||||
|
||||
iAdrszFormat1: .int szFormat1
|
||||
iAdrsfNumber: .int sfNumber
|
||||
iAdrsfNumber1: .int sfNumber1
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
BEGIN {
|
||||
r=7.125
|
||||
printf " %9.3f\n",-r
|
||||
printf " %9.3f\n",r
|
||||
printf " %-9.3f\n",r
|
||||
printf " %09.3f\n",-r
|
||||
printf " %09.3f\n",r
|
||||
printf " %-09.3f\n",r
|
||||
}
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
with Ada.Text_Io.Editing; use Ada.Text_Io.Editing;
|
||||
with Ada.Text_Io; use Ada.Text_Io;
|
||||
|
||||
procedure Zero_Fill is
|
||||
Pic_String: String := "<999999.99>";
|
||||
Pic : Picture := To_Picture(Pic_String);
|
||||
type Money is delta 0.01 digits 8;
|
||||
package Money_Output is new Decimal_Output(Money);
|
||||
use Money_Output;
|
||||
|
||||
Value : Money := 37.25;
|
||||
begin
|
||||
Put(Item => Value, Pic => Pic);
|
||||
end Zero_Fill;
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
o_form("/w9s0/\n", 7.125);
|
||||
o_form("/w12d6p6/\n", -12.0625);
|
||||
o_form("/w12d6p6/\n", 7.125);
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
PROC newRealF(es, fl, digit, len=0, zeros=TRUE)
|
||||
DEF s, t, i
|
||||
IF (len = 0) OR (len < (digit+3))
|
||||
RETURN RealF(es, fl, digit)
|
||||
ELSE
|
||||
s := String(len)
|
||||
t := RealF(es, fl, digit)
|
||||
FOR i := 0 TO len-EstrLen(t)-1 DO StrAdd(s, IF zeros THEN '0' ELSE ' ')
|
||||
StrAdd(s, t)
|
||||
StrCopy(es, s)
|
||||
DisposeLink(s)
|
||||
DisposeLink(t)
|
||||
ENDIF
|
||||
ENDPROC es
|
||||
|
||||
PROC main()
|
||||
DEF s[100] : STRING
|
||||
WriteF('\s\n', newRealF(s, 7.125, 3,9))
|
||||
ENDPROC
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
r: 7.125
|
||||
|
||||
print r
|
||||
print to :string .format: "09.3f" r
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
MsgBox % pad(7.25,7) ; 0007.25
|
||||
MsgBox % pad(-7.25,7) ; -007.25
|
||||
|
||||
pad(x,len) { ; pad with 0's from left to len chars
|
||||
IfLess x,0, Return "-" pad(SubStr(x,2),len-1)
|
||||
VarSetCapacity(p,len,Asc("0"))
|
||||
Return SubStr(p x,1-len)
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
n = 7.125
|
||||
print rjust(string(n), 8, "0") # => 00007.125
|
||||
print zfill(string(n), 8) # => 00007.125
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
PRINT FNformat(PI, 9, 3)
|
||||
PRINT FNformat(-PI, 9, 3)
|
||||
END
|
||||
|
||||
DEF FNformat(n, sl%, dp%)
|
||||
LOCAL @%
|
||||
@% = &1020000 OR dp% << 8
|
||||
IF n >= 0 THEN
|
||||
= RIGHT$(STRING$(sl%,"0") + STR$(n), sl%)
|
||||
ENDIF
|
||||
= "-" + RIGHT$(STRING$(sl%,"0") + STR$(-n), sl%-1)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
' Formatted numeric output
|
||||
n = 7.125
|
||||
PRINT n FORMAT "%09.3f\n"
|
||||
|
|
@ -0,0 +1,77 @@
|
|||
/*
|
||||
* Print number n, using at least c characters.
|
||||
*
|
||||
* Different from normal, this function:
|
||||
* 1. Uses the current ibase (not the obase) to print the number.
|
||||
* 2. Prunes "0" digits from the right, so p(1.500, 1) prints "1.5".
|
||||
* 3. Pads "0" digits to the left, so p(-1.5, 6) prints "-001.5".
|
||||
* 4. Never prints a newline.
|
||||
*
|
||||
* Use an assignment, as t = p(1.5, 1), to discard the return value
|
||||
* from this function so that bc not prints the return value.
|
||||
*/
|
||||
define p(n, c) {
|
||||
auto d, d[], f, f[], i, m, r, s, v
|
||||
s = scale /* Save original scale. */
|
||||
|
||||
if (n < 0) {
|
||||
"-" /* Print negative sign. */
|
||||
c -= 1
|
||||
n = -n /* Remove negative sign from n. */
|
||||
}
|
||||
|
||||
/* d[] takes digits before the radix point. */
|
||||
scale = 0
|
||||
for (m = n / 1; m != 0; m /= 10) d[d++] = m % 10
|
||||
|
||||
/* f[] takes digits after the radix point. */
|
||||
r = n - (n / 1) /* r is these digits. */
|
||||
scale = scale(n)
|
||||
f = -1 /* f counts the digits of r. */
|
||||
for (m = r + 1; m != 0; m /= 10) f += 1
|
||||
scale = 0
|
||||
r = r * (10 ^ f) / 1 /* Remove radix point from r. */
|
||||
if (r != 0) {
|
||||
while (r % 10 == 0) { /* Prune digits. */
|
||||
f -= 1
|
||||
r /= 10
|
||||
}
|
||||
for (i = 0; i < f; i++) {
|
||||
f[i] = r % 10
|
||||
r /= 10
|
||||
}
|
||||
}
|
||||
|
||||
/* Pad "0" digits to reach c characters. */
|
||||
c -= d
|
||||
if (f > 0) c -= 1 + f
|
||||
for (1; c > 0; c--) "0" /* Print "0". */
|
||||
|
||||
/* i = index, m = maximum index, r = digit to print. */
|
||||
m = d + f
|
||||
for (i = 1; i <= m; i++) {
|
||||
if (i <= d) r = d[d - i]
|
||||
if (i > d) r = f[m - i]
|
||||
if (i == d + 1) "." /* Print radix point. */
|
||||
|
||||
v = 0
|
||||
if (r == v++) "0" /* Print digit. */
|
||||
if (r == v++) "1"
|
||||
if (r == v++) "2" /* r == 2 might not work, */
|
||||
if (r == v++) "3" /* unless ibase is ten. */
|
||||
if (r == v++) "4"
|
||||
if (r == v++) "5"
|
||||
if (r == v++) "6"
|
||||
if (r == v++) "7"
|
||||
if (r == v++) "8"
|
||||
if (r == v++) "9"
|
||||
if (r == v++) "A"
|
||||
if (r == v++) "B"
|
||||
if (r == v++) "C"
|
||||
if (r == v++) "D"
|
||||
if (r == v++) "E"
|
||||
if (r == v++) "F"
|
||||
}
|
||||
|
||||
scale = s /* Restore original scale. */
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
x = 7.125
|
||||
"Decimal: "; t = p(x, 9); "
|
||||
"
|
||||
ibase = 16
|
||||
"Hexadecimal: "; t = p(x, 9); "
|
||||
"
|
||||
ibase = 2
|
||||
"Binary: "; t = p(x, 1001); "
|
||||
"
|
||||
quit
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
beads 1 program 'Formatted numeric output'
|
||||
calc main_init
|
||||
var num = 7.125
|
||||
log to_str(num, min:9, zero_pad:Y)
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
#include <iostream>
|
||||
#include <iomanip>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::setfill('0') << std::setw(9) << std::fixed << std::setprecision(3) << 7.125 << std::endl;
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
#include <iostream>
|
||||
#include <format>
|
||||
|
||||
int main()
|
||||
{
|
||||
std::cout << std::format("{:09.3f}\n", 7.125);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
class Program
|
||||
{
|
||||
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
|
||||
float myNumbers = 7.125F;
|
||||
|
||||
string strnumber = Convert.ToString(myNumbers);
|
||||
|
||||
Console.WriteLine(strnumber.PadLeft(9, '0'));
|
||||
|
||||
Console.ReadLine();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
11
Task/Formatted-numeric-output/C/formatted-numeric-output.c
Normal file
11
Task/Formatted-numeric-output/C/formatted-numeric-output.c
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#include <stdio.h>
|
||||
main(){
|
||||
float r=7.125;
|
||||
printf(" %9.3f\n",-r);
|
||||
printf(" %9.3f\n",r);
|
||||
printf(" %-9.3f\n",r);
|
||||
printf(" %09.3f\n",-r);
|
||||
printf(" %09.3f\n",r);
|
||||
printf(" %-09.3f\n",r);
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
IDENTIFICATION DIVISION.
|
||||
PROGRAM-ID. NUMERIC-OUTPUT-PROGRAM.
|
||||
DATA DIVISION.
|
||||
WORKING-STORAGE SECTION.
|
||||
01 WS-EXAMPLE.
|
||||
05 X PIC 9(5)V9(3).
|
||||
PROCEDURE DIVISION.
|
||||
MOVE 7.125 TO X.
|
||||
DISPLAY X UPON CONSOLE.
|
||||
STOP RUN.
|
||||
|
|
@ -0,0 +1 @@
|
|||
10 print using "0000#.###";7.125
|
||||
|
|
@ -0,0 +1 @@
|
|||
(cl-format true "~9,3,,,'0F" 7.125)
|
||||
|
|
@ -0,0 +1 @@
|
|||
(printf "%09.3f" 7.125) ; format works the same way (without side the effect of printing)
|
||||
|
|
@ -0,0 +1 @@
|
|||
(format t "~9,3,,,'0F" 7.125)
|
||||
11
Task/Formatted-numeric-output/D/formatted-numeric-output.d
Normal file
11
Task/Formatted-numeric-output/D/formatted-numeric-output.d
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import std.stdio;
|
||||
|
||||
void main() {
|
||||
immutable r = 7.125;
|
||||
writefln(" %9.3f", -r);
|
||||
writefln(" %9.3f", r);
|
||||
writefln(" %-9.3f", r);
|
||||
writefln(" %09.3f", -r);
|
||||
writefln(" %09.3f", r);
|
||||
writefln(" %-09.3f", r);
|
||||
}
|
||||
|
|
@ -0,0 +1,12 @@
|
|||
D5=7125
|
||||
A10=D5,'-ZZZZX.XXX' ; 7.125
|
||||
A10=D5,'-ZZZZX.XXX' [LEFT] ;7.125
|
||||
A10=D5,'-XXXXX.XXX' ; 00007.125
|
||||
A10=-D5,'-ZZZZX.XXX' ;- 7.125
|
||||
A10=-D5,'-ZZZZX.XXX' [LEFT] ;- 7.125
|
||||
A10=-D5,'-XXXXX.XXX' [LEFT] ;-00007.125
|
||||
A10=-D5,'XXXXX.XXX-' ;00007.125-
|
||||
A10=-D5,'ZZZZX.XXX-' ; 7.125-
|
||||
A10=-D5,'ZZZZX.XXX-' [LEFT] ;7.125-
|
||||
|
||||
A10=1500055,'ZZZ,ZZX.XX ; 15,000.55
|
||||
112
Task/Formatted-numeric-output/Dc/formatted-numeric-output-1.dc
Normal file
112
Task/Formatted-numeric-output/Dc/formatted-numeric-output-1.dc
Normal file
|
|
@ -0,0 +1,112 @@
|
|||
[*
|
||||
* (n) (c) lpx
|
||||
* Print number n, using at least c characters.
|
||||
*
|
||||
* Different from normal, this function:
|
||||
* 1. Uses the current ibase (not the obase) to print the number.
|
||||
* 2. Prunes "0" digits from the right, so [1.500 1 lxp] prints "1.5".
|
||||
* 3. Pads "0" digits to the left, so [_1.5 6 lxp] prints "-001.5".
|
||||
* 4. Never prints a newline.
|
||||
*]sz
|
||||
[
|
||||
Sc Sn [Local n, c = from stack.]sz
|
||||
K Ss [Local s = original scale.]sz
|
||||
[Reserve local variables D, F, I, L.]sz
|
||||
0 SD 0 SF 0 SI 0 SL
|
||||
|
||||
[ [If n < 0:]sz
|
||||
[-]P [Print negative sign.]sz
|
||||
lc 1 - sc [Decrement c.]sz
|
||||
0 ln - sn [Negate n.]sz
|
||||
]sI 0 ln <I
|
||||
|
||||
[*
|
||||
* Array D[] takes digits before the radix point.
|
||||
*]sz
|
||||
0 k [scale = 0]sz
|
||||
0 Sd [Local d = 0]sz
|
||||
ln 1 / [Push digits before radix point.]sz
|
||||
[ [Loop to fill D[]:]sz
|
||||
d 10 % ld :D [D[d] = next digit.]sz
|
||||
ld 1 + sd [Increment d.]sz
|
||||
10 / [Remove digit.]sz
|
||||
d 0 !=L [Loop until no digits.]sz
|
||||
]sL d 0 !=L
|
||||
sz [Pop digits.]sz
|
||||
|
||||
[*
|
||||
* Array F[] takes digits after the radix point.
|
||||
*]sz
|
||||
ln ln 1 / - [Push digits after radix point.]sz
|
||||
d X k [scale = enough.]sz
|
||||
_1 Sf [Local f = -1]sz
|
||||
d 1 + [Push 1 + digits after radix point.]sz
|
||||
[ [Loop to count digits:]sz
|
||||
lf 1 + sf [Increment f.]sz
|
||||
10 / [Remove digit.]sz
|
||||
d 0 !=L [Loop until no digits.]sz
|
||||
]sL d 0 !=L
|
||||
sz [Pop 1 + digits.]sz
|
||||
0 k [scale = 0]sz
|
||||
10 lf ^ * 1 / [Remove radix point from digits.]sz
|
||||
[ [Loop to prune digits:]sz
|
||||
lf 1 - sf [Decrement f.]sz
|
||||
10 / [Remove digit.]sz
|
||||
d 10 % 0 =L [Loop while last digit is 0.]sz
|
||||
]sL d 10 % 0 =L
|
||||
0 Si [Local i = 0]sz
|
||||
[ [Loop to fill F[]:]sz
|
||||
d 10 % li :F [F[i] = next digit.]sz
|
||||
10 / [Remove digit.]sz
|
||||
li 1 + si [Increment i.]sz
|
||||
lf li <L [Loop while i < f.]sz
|
||||
]sL lf li <L
|
||||
sz [Pop digits.]sz
|
||||
|
||||
lc ld - [Push count = c - d.]sz
|
||||
[ [If f > 0:]sz
|
||||
1 lf + - [Subtract 1 radix point + f from count.]sz
|
||||
]sI 0 lf >I
|
||||
[ [Loop:]sz
|
||||
[0]P [Print a padding "0".]sz
|
||||
1 - [Decrement count.]sz
|
||||
d 0 <L [Loop while count > 0.]sz
|
||||
]sL d 0 <L
|
||||
sz [Pop count.]sz
|
||||
|
||||
[ [Local function (digit) lPx:]sz
|
||||
[ [Execute:]sz
|
||||
[*
|
||||
* Push the string that matches the digit.
|
||||
*]sz
|
||||
[[0] 2Q]sI d 0 =I [[1] 2Q]sI d 1 =I [[2] 2Q]sI d 2 =I [[3] 2Q]sI d 3 =I
|
||||
[[4] 2Q]sI d 4 =I [[5] 2Q]sI d 5 =I [[6] 2Q]sI d 6 =I [[7] 2Q]sI d 7 =I
|
||||
[[8] 2Q]sI d 8 =I [[9] 2Q]sI d 9 =I [[A] 2Q]sI d A =I [[B] 2Q]sI d B =I
|
||||
[[C] 2Q]sI d C =I [[D] 2Q]sI d D =I [[E] 2Q]sI d E =I [[F] 2Q]sI d F =I
|
||||
[?] [Else push "?".]sz
|
||||
]x
|
||||
P [Print the string.]sz
|
||||
sz [Pop the digit.]sz
|
||||
]SP
|
||||
ld [Push counter = d.]sz
|
||||
[ [Loop:]sz
|
||||
1 - [Decrement counter.]sz
|
||||
d ;D lPx [Print digit D[counter].]sz
|
||||
d 0 <L [Loop while counter > 0.]sz
|
||||
]sL d 0 <L
|
||||
sz [Pop counter.]sz
|
||||
[ [If f > 0:]sz
|
||||
[.]P [Print radix point.]sz
|
||||
lf [Push counter = f.]sz
|
||||
[ [Loop:]sz
|
||||
1 - [Decrement counter.]sz
|
||||
d ;F lPx [Print digit F[counter].]sz
|
||||
d 0 <L [Loop while counter > 0.]sz
|
||||
]sL d 0 <L
|
||||
sz [Pop counter.]sz
|
||||
]sI 0 lf >I
|
||||
|
||||
[Restore variables n, c, d, f, D, F, L, I, P.]sz
|
||||
Lnsz Lcsz Ldsz Lfsz LDsz LFsz LLsz LIsz LPsz
|
||||
Ls k [Restore variable s. Restore original scale.]sz
|
||||
]sp
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
7.125 sx
|
||||
[Decimal: ]P lx 9 lpx [
|
||||
]P 16 i [Hexadecimal: ]P lx 9 lpx [
|
||||
]P 2 i [Binary: ]P lx 9 lpx [
|
||||
]P
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
program FormattedNumericOutput;
|
||||
|
||||
{$APPTYPE CONSOLE}
|
||||
|
||||
uses
|
||||
SysUtils;
|
||||
|
||||
const
|
||||
fVal = 7.125;
|
||||
|
||||
begin
|
||||
Writeln(FormatFloat('0000#.000',fVal));
|
||||
Writeln(FormatFloat('0000#.0000000',fVal));
|
||||
Writeln(FormatFloat('##.0000000',fVal));
|
||||
Writeln(FormatFloat('0',fVal));
|
||||
Writeln(FormatFloat('#.#E-0',fVal));
|
||||
Writeln(FormatFloat('#,##0.00;;Zero',fVal));
|
||||
Readln;
|
||||
end.
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
PROGRAM FORMATTED
|
||||
|
||||
PROCEDURE FORMATTED_PRINT(N,LENGTH,DEC_PLACES->FP$)
|
||||
|
||||
LOCAL I,C$,NN$
|
||||
|
||||
FORMAT$=STRING$(LENGTH,"#")+"."
|
||||
|
||||
FOR I=1 TO DEC_PLACES DO
|
||||
FORMAT$=FORMAT$+"#"
|
||||
END FOR
|
||||
|
||||
OPEN("O",1,"FORMAT.$$$")
|
||||
WRITE(#1,FORMAT$;N)
|
||||
CLOSE(1)
|
||||
|
||||
OPEN("I",1,"FORMAT.$$$")
|
||||
INPUT(LINE,#1,N$)
|
||||
CLOSE(1)
|
||||
|
||||
! add leading zeros
|
||||
FOR I=1 TO LEN(N$) DO
|
||||
C$=MID$(N$,I,1)
|
||||
IF C$=" " OR C$="%" THEN NN$=NN$+"0" ELSE NN$=NN$+C$
|
||||
END FOR
|
||||
|
||||
FP$=RIGHT$("000000000000"+NN$,LENGTH) ! chop to required length
|
||||
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
|
||||
PRINT(CHR$(12);) ! CLS
|
||||
|
||||
FOR I=1 TO 10 DO
|
||||
N=RND(1)*10^(INT(10*RND(1))-2)
|
||||
FORMATTED_PRINT(N,16,5->FP$)
|
||||
PRINT("Raw number =";N;TAB(30);"Using custom function =";FP$)
|
||||
END FOR
|
||||
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,67 @@
|
|||
note
|
||||
description : "{
|
||||
2 Examples are given.
|
||||
The first example uses the standard library's FORMAT_DOUBLE class.
|
||||
The second example uses the AEL_PRINTF class from the freely available
|
||||
Amalasoft Eiffel Library (AEL).
|
||||
|
||||
See additional comments in the code.
|
||||
}"
|
||||
|
||||
class APPLICATION
|
||||
|
||||
inherit
|
||||
AEL_PRINTF -- Optional, see below
|
||||
|
||||
create
|
||||
make
|
||||
|
||||
feature {NONE} -- Initialization
|
||||
|
||||
make
|
||||
-- Run application.
|
||||
do
|
||||
print_formatted_std (7.125)
|
||||
print_formatted_ael (7.125)
|
||||
end
|
||||
|
||||
--|--------------------------------------------------------------
|
||||
|
||||
print_formatted_std (v: REAL_64)
|
||||
-- Print the value 'v' as a zero-padded string in a fixed
|
||||
-- overall width of 9 places and, with a precision of
|
||||
-- to 3 places to the right of the decimal point.
|
||||
-- Use the FORMAT_DOUBLE class from the standard library
|
||||
local
|
||||
fmt: FORMAT_DOUBLE
|
||||
do
|
||||
create fmt.make (9, 3)
|
||||
fmt.zero_fill
|
||||
print (fmt.formatted (v) + "%N")
|
||||
end
|
||||
|
||||
--|--------------------------------------------------------------
|
||||
|
||||
print_formatted_ael (v: REAL_64)
|
||||
-- Print the value 'v' as a zero-padded string in a fixed
|
||||
-- overall width of 9 places and, with a precision of
|
||||
-- to 3 places to the right of the decimal point.
|
||||
-- Use the AEL_PRINTF class from the Amalasoft Eiffel Library
|
||||
-- freely available from www.amalasoft.com
|
||||
do
|
||||
-- printf accepts a format string and an argument list
|
||||
-- The argument list is a container (often a manifest
|
||||
-- array) of values corresponding to the type of the format
|
||||
-- specified in the format string argument.
|
||||
-- When only one argument is needed, then there is also the
|
||||
-- option to use just the value, without the container.
|
||||
-- In this example, the line would be:
|
||||
-- printf ("%%09.3f%N", v)
|
||||
-- The more deliberate form is used in the actual example,
|
||||
-- as it is more representative of common usage, when there
|
||||
-- are multiple value arguments.
|
||||
|
||||
printf ("%%09.3f%N", << v >>)
|
||||
end
|
||||
|
||||
end
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
n = 7.125
|
||||
:io.fwrite "~f~n", [n]
|
||||
:io.fwrite "~.3f~n", [n]
|
||||
:io.fwrite "~9f~n", [n]
|
||||
:io.fwrite "~9.3f~n", [n]
|
||||
:io.fwrite "~9..0f~n", [n]
|
||||
:io.fwrite "~9.3.0f~n", [n]
|
||||
:io.fwrite "~9.3._f~n", [n]
|
||||
:io.fwrite "~f~n", [-n]
|
||||
:io.fwrite "~9.3f~n", [-n]
|
||||
:io.fwrite "~9.3.0f~n", [-n]
|
||||
:io.fwrite "~e~n", [n]
|
||||
:io.fwrite "~12.4e~n", [n]
|
||||
:io.fwrite "~12.4.0e~n", [n]
|
||||
|
|
@ -0,0 +1 @@
|
|||
(format "%09.3f" 7.125) ;=> "00007.125"
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
constant r = 7.125
|
||||
printf(1,"%9.3f\n",-r)
|
||||
printf(1,"%9.3f\n",r)
|
||||
printf(1,"%-9.3f\n",r)
|
||||
printf(1,"%09.3f\n",-r)
|
||||
printf(1,"%09.3f\n",r)
|
||||
printf(1,"%-09.3f\n",r)
|
||||
|
|
@ -0,0 +1 @@
|
|||
printfn "%09.3f" 7.125f
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
USE: formatting
|
||||
7.125 "%09.3f\n" printf
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
class Main
|
||||
{
|
||||
public static Void main()
|
||||
{
|
||||
echo (7.125.toStr.padl(9, '0'))
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
\ format 'n' digits of the double word 'd'
|
||||
: #n ( d n -- d ) 0 ?do # loop ;
|
||||
|
||||
\ ud.0 prints an unsigned double
|
||||
: ud.0 ( d n -- ) <# 1- #n #s #> type ;
|
||||
|
||||
\ d.0 prints a signed double
|
||||
: d.0 ( d n -- ) >r tuck dabs <# r> 1- #n #s rot sign #> type ;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
Type: 123 s>d 8 ud.0
|
||||
Result: 00000123 ok
|
||||
Type: -123 s>d 8 d.0
|
||||
Result: -00000123 ok
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
INTEGER :: number = 7125
|
||||
WRITE(*,"(I8.8)") number ! Prints 00007125
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
INTEGER IV
|
||||
REAL V
|
||||
DATA V/7.125/ !A positive number.
|
||||
IV = V !Grab the integer part.
|
||||
WRITE (6,1) V,IV
|
||||
1 FORMAT (F9.3,T1,I5.5)
|
||||
END
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
#Include "vbcompat.bi"
|
||||
|
||||
Dim s As String = Format(7.125, "00000.0##")
|
||||
Print s
|
||||
Sleep
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
window 1, @"Formatted Numeric Output", (0,0,480,270)
|
||||
|
||||
print using "0000#.###";7.125
|
||||
|
||||
HandleEvents
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
Public Sub Main()
|
||||
|
||||
Print Format("7.125", "00000.000")
|
||||
|
||||
End
|
||||
|
|
@ -0,0 +1 @@
|
|||
print sprintf("%09.3f", 7.125)
|
||||
|
|
@ -0,0 +1 @@
|
|||
fmt.Printf("%09.3f", 7.125)
|
||||
|
|
@ -0,0 +1 @@
|
|||
printf ("%09.3f", 7.125)
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
import Text.Printf
|
||||
main =
|
||||
printf "%09.3f" 7.125
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
fun format n length
|
||||
let n tostr n
|
||||
while len n < length
|
||||
let n 0 + n
|
||||
endwhile
|
||||
println n
|
||||
endfun
|
||||
|
||||
format 7.125 9
|
||||
|
|
@ -0,0 +1 @@
|
|||
WRITE(ClipBoard, Format='i5.5, F4.3') INT(7.125), MOD(7.125, 1) ! 00007.125
|
||||
21
Task/Formatted-numeric-output/I/formatted-numeric-output.i
Normal file
21
Task/Formatted-numeric-output/I/formatted-numeric-output.i
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
concept FixedLengthFormat(value, length) {
|
||||
string = text(abs(value))
|
||||
prefix = ""
|
||||
sign = ""
|
||||
|
||||
if value < 0
|
||||
sign = "-"
|
||||
end
|
||||
|
||||
if #string < length
|
||||
prefix = "0"*(length-#sign-#string-#prefix)
|
||||
end
|
||||
|
||||
return sign+prefix+string
|
||||
}
|
||||
|
||||
software {
|
||||
d = 7.125
|
||||
print(FixedLengthFormat(d, 9))
|
||||
print(FixedLengthFormat(-d, 9))
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
n = 7.125
|
||||
print, n, format='(f08.3)'
|
||||
;==> 0007.125
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
100 LET F=7.125
|
||||
110 PRINT USING "-%%%%%.###":F
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
link printf
|
||||
|
||||
procedure main()
|
||||
|
||||
every r := &pi | -r | 100-r do {
|
||||
write(r," <=== no printf")
|
||||
every p := "|%r|" | "|%9.3r|" | "|%-9.3r|" | "|%0.3r|" | "|%e|" | "|%d|" do
|
||||
write(sprintf(p,r)," <=== sprintf ",p)
|
||||
}
|
||||
end
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
'r<0>9.3' (8!:2) 7.125
|
||||
00007.125
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
public class Printing{
|
||||
public static void main(String[] args){
|
||||
double value = 7.125;
|
||||
System.out.printf("%09.3f",value); // System.out.format works the same way
|
||||
System.out.println(String.format("%09.3f",value));
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,21 @@
|
|||
import java.text.DecimalFormat;
|
||||
import java.text.NumberFormat;
|
||||
|
||||
public class Format {
|
||||
public static void main(String[] args){
|
||||
NumberFormat numForm = new DecimalFormat();
|
||||
numForm.setMinimumIntegerDigits(9);
|
||||
//Maximum also available for Integer digits and Fraction digits
|
||||
numForm.setGroupingUsed(false);//stops it from inserting commas
|
||||
System.out.println(numForm.format(7.125));
|
||||
|
||||
//example of Fraction digit options
|
||||
numForm.setMinimumIntegerDigits(5);
|
||||
numForm.setMinimumFractionDigits(5);
|
||||
System.out.println(numForm.format(7.125));
|
||||
numForm.setMinimumFractionDigits(0);
|
||||
numForm.setMaximumFractionDigits(2);
|
||||
System.out.println(numForm.format(7.125));
|
||||
System.out.println(numForm.format(7.135));//rounds to even
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
var n = 123;
|
||||
var str = ("00000" + n).slice(-5);
|
||||
alert(str);
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
def pp0(width):
|
||||
tostring
|
||||
| if width > length then (width - length) * "0" + . else . end;
|
||||
|
||||
# pp(left; right) formats a decimal number to occupy
|
||||
# (left+right+1) positions if possible,
|
||||
# where "left" is the number of characters to the left of
|
||||
# the decimal point, and similarly for "right".
|
||||
def pp(left; right):
|
||||
def lpad: if (left > length) then ((left - length) * "0") + . else . end;
|
||||
tostring as $s
|
||||
| $s
|
||||
| index(".") as $ix
|
||||
| ((if $ix then $s[0:$ix] else $s end) | lpad) + "." +
|
||||
(if $ix then $s[$ix+1:] | .[0:right] else "" end);
|
||||
|
|
@ -0,0 +1 @@
|
|||
(1.0, 12.3, 333.333, 1e6) | pp0(10)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
0000000001
|
||||
00000012.3
|
||||
000333.333
|
||||
0001000000
|
||||
|
|
@ -0,0 +1 @@
|
|||
(1.0, 12.3, 333.333, 1e6) | pp(4;2)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
0001.
|
||||
0012.3
|
||||
0333.33
|
||||
1000000.
|
||||
|
|
@ -0,0 +1,15 @@
|
|||
using Printf
|
||||
test = [7.125, [rand()*10^rand(0:4) for i in 1:9]]
|
||||
|
||||
println("Formatting some numbers with the @sprintf macro (using \"%09.3f\"):")
|
||||
for i in test
|
||||
println(@sprintf " %09.3f" i)
|
||||
end
|
||||
|
||||
using Formatting
|
||||
println()
|
||||
println("The same thing using the Formatting package:")
|
||||
fe = FormatExpr(" {1:09.3f}")
|
||||
for i in test
|
||||
printfmtln(fe, i)
|
||||
end
|
||||
|
|
@ -0,0 +1,6 @@
|
|||
// version 1.0.5-2
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val num = 7.125
|
||||
println("%09.3f".format(num))
|
||||
}
|
||||
|
|
@ -0,0 +1,30 @@
|
|||
{def fmt
|
||||
{def padd {lambda {:n :x} {if {< :n 1} then else :x{padd {- :n 1} :x}}}}
|
||||
{def trunc {lambda {:n} {if {> :n 0} then {floor :n} else {ceil :n}}}}
|
||||
{lambda {:a :b :n}
|
||||
{let { {:a :a} {:b :b} {:n {abs :n}} {:sign {if {>= :n 0} then + else -}}
|
||||
{:int {trunc :n}}
|
||||
{:dec {ceil {* 1.0e:b {abs {- :n {trunc :n}}}}} }
|
||||
} {br}{padd {- :a {W.length {trunc :n}}} >}
|
||||
{if {W.equal? :sign -} then else :sign}:int.:dec{padd {- :b {W.length :dec}} 0} }}}
|
||||
-> fmt
|
||||
|
||||
{def numbers
|
||||
7.125
|
||||
10.7
|
||||
0.980
|
||||
-1000
|
||||
559.8
|
||||
-69.99
|
||||
4970.430}
|
||||
-> numbers
|
||||
|
||||
{S.map {fmt 10 3} {numbers}}
|
||||
->
|
||||
>>>>>>>>> +7.125
|
||||
>>>>>>>> +10.699
|
||||
>>>>>>>>> +0.980
|
||||
>>>>>> -1000.000
|
||||
>>>>>>> +559.799
|
||||
>>>>>>>> -69.990
|
||||
>>>>>> +4970.430
|
||||
|
|
@ -0,0 +1 @@
|
|||
7.125 -> asstring(-precision = 3, -padding = 9, -padchar = '0')
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
for i = 1 to 10
|
||||
n = rnd(1) * 10 ^ (int(10 * rnd(1)) - 2)
|
||||
print "Raw number = "; n, "Using custom function = "; FormattedPrint$(n, 16, 5)
|
||||
next i
|
||||
end
|
||||
|
||||
function FormattedPrint$(n, length, decPlaces)
|
||||
format$ = "#."
|
||||
for i = 1 to decPlaces
|
||||
format$ = format$ + "#"
|
||||
next i
|
||||
|
||||
n$ = using(format$, n) ' remove leading spaces if less than 3 figs left of decimal
|
||||
' add leading zeros
|
||||
for i = 1 to len(n$)
|
||||
c$ = mid$(n$, i, 1)
|
||||
if c$ = " " or c$ = "%" then nn$ = nn$ + "0" else nn$ = nn$ + c$
|
||||
next i
|
||||
FormattedPrint$ = right$( "000000000000" +nn$, length) ' chop to required length
|
||||
end function
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
to zpad :num :width :precision
|
||||
output map [ifelse ? = "| | ["0] [?]] form :num :width :precision
|
||||
end
|
||||
print zpad 7.125 9 3 ; 00007.125
|
||||
|
|
@ -0,0 +1 @@
|
|||
print form 7.125 -1 "|%09.3f| ; 00007.125
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
function digits(n) return math.floor(math.log(n) / math.log(10))+1 end
|
||||
function fixedprint(num, digs) --digs = number of digits before decimal point
|
||||
for i = 1, digs - digits(num) do
|
||||
io.write"0"
|
||||
end
|
||||
print(num)
|
||||
end
|
||||
|
||||
fixedprint(7.125, 5) --> 00007.125
|
||||
|
|
@ -0,0 +1 @@
|
|||
print(string.format("%09.3d",7.125))
|
||||
|
|
@ -0,0 +1 @@
|
|||
Print str$(7.125,"00000.000")
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
>> disp(sprintf('%09.3f',7.125))
|
||||
00007.125
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
printf("%f", Pi);
|
||||
3.141593
|
||||
printf("%.0f", Pi);
|
||||
3
|
||||
printf("%.2f", Pi);
|
||||
3.14
|
||||
printf("%08.2f", Pi);
|
||||
00003.14
|
||||
printf("%8.2f", Pi);
|
||||
3.14
|
||||
printf("%-8.2f|", Pi);
|
||||
3.14 |
|
||||
printf("%+08.2f", Pi);
|
||||
+0003.14
|
||||
printf("%+0*.*f",8, 2, Pi);
|
||||
+0003.14
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
StringTake["000000" <> ToString[7.125], -9]
|
||||
00007.125
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
:- module formatted_numeric_output.
|
||||
:- interface.
|
||||
:- import_module io.
|
||||
|
||||
:- pred main(io::di, io::uo) is det.
|
||||
|
||||
:- implementation.
|
||||
:- import_module list, string.
|
||||
|
||||
main(!IO) :-
|
||||
io.format("%09.3f\n", [f(7.125)], !IO).
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
(pick length - repeat prefix) ^left-pad
|
||||
|
||||
7.125 string "0" 9 left-pad puts!
|
||||
|
|
@ -0,0 +1 @@
|
|||
IO.Put(Fmt.Pad("7.125\n", length := 10, padChar := '0'));
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
printer = 7.125
|
||||
println format("%09.3f", printer)
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
10 REM Formatted numeric output
|
||||
20 CLS
|
||||
30 LET N=7.125
|
||||
40 LET W=9
|
||||
50 GOSUB 1000
|
||||
60 SCREEN 10,9:PRINT N$
|
||||
70 END
|
||||
1000 REM Formatted fixed-length
|
||||
1010 N$=STR$(N):N$=RIGHT$(N$,LEN(N$)-1)
|
||||
1020 FOR I=1 TO W-LEN(N$)
|
||||
1030 N$="0"+N$
|
||||
1040 NEXT I
|
||||
1050 RETURN
|
||||
|
|
@ -0,0 +1,16 @@
|
|||
/* NetRexx */
|
||||
|
||||
options replace format comments java crossref savelog symbols binary
|
||||
|
||||
import java.text.MessageFormat
|
||||
|
||||
sevenPointOneTwoFive = double 7.125
|
||||
|
||||
-- using NetRexx Built-In Functions (BIFs)
|
||||
say Rexx(sevenPointOneTwoFive).format(5, 3).changestr(' ', '0')
|
||||
|
||||
-- using Java library constructs
|
||||
System.out.printf('%09.3f\n', [Double(sevenPointOneTwoFive)])
|
||||
say MessageFormat.format('{0,number,#00000.###}', [Double(sevenPointOneTwoFive)])
|
||||
|
||||
return
|
||||
|
|
@ -0,0 +1,7 @@
|
|||
import strformat
|
||||
const r = 7.125
|
||||
echo r
|
||||
echo fmt"{-r:9.3f}"
|
||||
echo fmt"{r:9.3f}"
|
||||
echo fmt"{-r:09.3f}"
|
||||
echo fmt"{r:09.3f}"
|
||||
|
|
@ -0,0 +1 @@
|
|||
Printf.printf "%09.3f\n" 7.125
|
||||
|
|
@ -0,0 +1 @@
|
|||
Out.Real(7.125, 9, 0);
|
||||
|
|
@ -0,0 +1 @@
|
|||
NSLog(@"%09.3f", 7.125);
|
||||
|
|
@ -0,0 +1 @@
|
|||
NSLog(@"%@", [NSString stringWithFormat:@"%09.3f", 7.125]);
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
MESSAGE
|
||||
STRING( 7.125, "99999.999" )
|
||||
VIEW-AS ALERT-BOX.
|
||||
11
Task/Formatted-numeric-output/Oz/formatted-numeric-output.oz
Normal file
11
Task/Formatted-numeric-output/Oz/formatted-numeric-output.oz
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
declare
|
||||
fun {PrintFloat X Prec}
|
||||
{Property.put 'print.floatPrecision' Prec}
|
||||
S = {Float.toString X}
|
||||
in
|
||||
{Append
|
||||
for I in 1..Prec-{Length S}+1 collect:C do {C &0} end
|
||||
S}
|
||||
end
|
||||
in
|
||||
{System.showInfo {PrintFloat 7.125 8}}
|
||||
|
|
@ -0,0 +1 @@
|
|||
printf("%09.4f\n", Pi)
|
||||
|
|
@ -0,0 +1 @@
|
|||
echo str_pad(7.125, 9, '0', STR_PAD_LEFT);
|
||||
|
|
@ -0,0 +1 @@
|
|||
printf("%09.3f\n", 7.125);
|
||||
|
|
@ -0,0 +1,3 @@
|
|||
put edit (X) (p'999999.V999'); /* Western format. */
|
||||
|
||||
put edit (X) (p'999999,V999'); /* In European format. */
|
||||
|
|
@ -0,0 +1,25 @@
|
|||
lz: Proc Options(main);
|
||||
/*********************************************************************
|
||||
* 10.09.2013 Walter Pachl one way to treat negative numbers
|
||||
* another would be using a Picture of 'S(9)9.V(3)9' or '-(9)9.V(3)9'
|
||||
*********************************************************************/
|
||||
Call z2lz(1.2);
|
||||
Call z2lz(-1.32);
|
||||
Call z2lz(123456789.012);
|
||||
Call z2lz(-23456789.012);
|
||||
Call z2lz(-123456789.012);
|
||||
|
||||
z2lz: Proc(z);
|
||||
Dcl z Dec Fixed(15,3); ;
|
||||
Dcl s Char(13) Based(addr(p));
|
||||
Dcl p Pic'(9)9.V(3)9';
|
||||
p=z;
|
||||
If z<0 Then
|
||||
If left(s,1)='0' Then substr(s,1,1)='-';
|
||||
Else Do;
|
||||
Put Skip List(z,'can''t be formatted that way');
|
||||
Return;
|
||||
End;
|
||||
Put Skip List(z,s);
|
||||
End;
|
||||
End;
|
||||
Some files were not shown because too many files have changed in this diff Show more
Loading…
Add table
Add a link
Reference in a new issue