Format decimal numbers in file I/O

Does anyone know how (if it's possible) to format decimals in Rule 5.1 file streams?

Example: '4.567890' formatted to print in file as '4.57', or even '04.57', etc.

Must this be done by brute force, or is there a format method or spec that can be used, and if so, how is it applied?

I have found no mention whatsoever in documentation of any formatting.

One way is to use a Decimal Variable (called d below) and a Number Variable (called i below) . If you want 2 decimal points you'd do this, assuming the value is in the decimal variable (d)

Set i to d * 100
Set d to i / 100

Setting the integer variable lops off the decimal portion of the decimal number, which has been multiplied by 100, leaving the two decimals. Then, dividing by 100 returns it to decimal format.

You can see how it will transform 23.456779 to 23.45. These use Set Variable with Variable Math option.

Is it possible to round up to the nearest hundredth? As in the example provided by @hub2?

Thanks Bruce!
This is the "brute force" method I alluded to. I was wondering is there is a more succinct method, but I will try this. The down-side is that the result will be truncated to 'n' decimal places, whereas if the *X number is a decimal and is round()ed before dividing by ten, it should be rounded up as appropriate.

Additionally, if stringizing methods are available, is it possible to create the line as a string and use format specs to insert multiple numbers in the line?

Could one use var.toFixed(2) for multiple decimal numbers into a string, or something of that nature?

Variable Math also has round operator:

This could be done before the truncation, extra steps...

By writing custom Groovy apps: yes. In RM: no.

1 Like

Adding .005 to your number before the * and / sequence will round up to the nearest hundredth.

2 Likes

Definitely. I've done programs with arithmetic in integer-only, producing up to 4 decimal places in the result, but maintained integer all the way through, including storage.

In the end, I have 3 numbers to enter into a file. One of the numbers is money, so I wanted to control format to prevent things like '$2.20' from being printed as '2.2', which is what happens, although not often. None of these techniques can fix that, I'm afraid. I also have a number that is 'days', and often comes to something like '30.12345678' and such.

In the end, I think I'll just be content with whatever the default formatting produces, since it's just academic to pretty the output, and the final numbers don't especially matter. They're just for historic review.

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.