How do I format decimals as string for writing to file?

Discussion in 'Rebol' started by gordon, Jun 10, 2011.

  1. gordon

    gordon New Member

    How do I format decimals as strings for writing to file?

    In other languages like C, Java and Python, there is some kind of string formatting feature that allows the developer to format numerical and other values as strings - for example in Python ...

    >> a=193.4
    >> print "%8.3f" % a
    >> 193.400


    Does this feature exist in REBOL?

    I am unable to find any documentation that describes it and it seems like such a basic, core feature of a programming language that I feel sure I must be missing it somewhere. Is there perhaps an easy way to do this in some other REBOL idiom that makes this feature unnecessary?

    Thanks

    Gordon
  2. Graham

    Graham Developer Staff Member

  3. MaxV

    MaxV Member

    It depends to what you want.
    I think you want to use save and load functions:
    Example:
    Code:
    a: ["I" "me" "irene" $34  10  5.4]
    save %output.txt a
    
    The file output.text will be:
    Code:
    "I" "me" "irene" $34.00 10 5.4
    
    If you use load:
    Code:
    b: load %output.txt
    == ["I" "me" "irene" $34  10  5.4]
    
    If you use save and load functions, you'll never loss the type of the value:
    Code:
    foreach item b [ print type? item]
    ==
    string
    string
    string
    money
    integer
    decimal
    
    Rebol make you free to specify the type of variable, it auto-recognize all! :D (fighting software complexity!)
    However you can transform the type of variable with the following functions:

    to-hex
    to-local-file
    to-rebol-file
    to-word
    to-function
    to-closure
    to-path
    to-lit-word
    to-get-word
    to-string
    to-file
    to-relative-file
    to-char
    to-error
    to-datatype
    to-library
    to-port
    to-set-word
    to-refinement
    to-none
    to-logic
    to-integer
    to-decimal
    to-money
    to-time
    to-date
    to-pair
    to-tuple
    to-bitset
    to-issue
    to-binary
    to-email
    to-url
    to-tag
    to-image
    to-block
    to-paren
    to-set-path
    to-lit-path
    to-hash
    to-list
    to-map
    to-get-path
    to-typeset
    to-itime
    to-idate


    I think that you should see this rebol IDE with all functions buttons grouped by type:
    http://www.rebol.org/view-script.r?script=rebolide.r
  4. MaxV

    MaxV Member

    On the other hand, you could see the mold function, it converts a value to a REBOL-readable string. This way they becomes string type:
    Code:
    >> mold [12 $4 "me"]
    == {[12 $4.00 "me"]}
    >> mold 12-12-2005
    == "12-Dec-2005"
    >> mold $4
    == "$4.00"
    >> type? mold $4
    == string!
    
  5. Graham

    Graham Developer Staff Member

    Gabriele also has a format decimal function ...
  6. MaxV

    MaxV Member

    If your question is: "How do I format decimals as strings for writing to file? "
    You don't need this on Rebol, you can write any type to a file. Just use write function.
    You can write also block and series!
    Code:
    write %temp.txt ["Hello Mr." 3]
    == "Hello Mr.3"
    

Share This Page