# Basic Question - Rounding Or Truncating A Number

## Recommended Posts

Again, sorry for such a basic question.  I have spent at least 45 minute trying to find this in the on-line manual.  I know the Floor(x) or Ceiling(x) will round to a whole number, but I need one decimal place to put into a displayed text value.  Note that I am actually concatenating the value to put into a static text display element. Currently, I am getting roughly one zillion decimal places.

Here is a theoretical example of something I am doing

X=20 // time in seconds

component.My_Text_Object.text = "The test will take " + (x/60) " Minutes."

But, it results in, "The test will take 0.333333333333 Minutes."

Is there a function to specify how many decimal places to get from a value? In Excel, I would use ROUNDUP(X,2) to have it display 0.33.  Is there a comparable function in DAQFactory.

-Joe

##### Share on other sites

Two choices:

1) use math:

floor(x/6) / 10

The standard math way to truncate to a particular spot is imply floor(x * 10^n) / 10^n where n is the number of decimals you want.

2) use Format(), for example:

format("%.1f", x/60)

Format works just like the C printf() statement and you can either search the internet for the specifiers, or look in the daqfactory user's guide.

##### Share on other sites

Thank you for that info.  For other readers' reference,

x=1.3333

floor(x*10)/10 will results in 1.3

-Joe

##### Share on other sites

As my line of code to do the formatting starts to get unreadable, I think I need to check into the format option a bit more.  However, I have not programmed in C, so the web pages I am finding for an explanation quickly lose me.

In the manual you give the example,

Format("My Value: %.3f",MyChannel[0])

returns "My Value 34392.492"

I am assuming in this example that the current value for MyChannel[0] is 34392.492....

Then, the "My Value: %.3f" both includes some text to include, plus adds some formatting info. This seems to be the rules that this follows;

"My Value  - that is the text to prefix the value.  Then there is a : as a separtor.

%.  - I don't know.

3   - Seems to indicate that the value should have 3 decimal places.

f    - I don't know.

Does this example round the value, or just truncate it to 3 decimal places?

What is the % and f for?

-Joe

##### Share on other sites

% is the indicator that you are providing a specifier to insert a value.  So, "My Value: " is all just text that gets copied.  The % says start a specifier.  The . means here's the decimal place.  The 3 means 3 numbers past the decimal.  The "f" means floating point.  It is rounded.

There are lots of other options.  For example, if you wanted to make a 4 digit hex number that always had 4 digits, even if the number was just 1, you'd do:

format("%04X", myValue)

If myValue was 1, you'd get 0001

You can do the same with float:

? format("%09.3f", 843.2348)

displays:

00843.235

The 0 after the % means prepend with 0's.  The 9 gives the total, minimum number of characters to generate (thus the reason we get 2 0's in front).  The rest is as before.

It really just takes practice to use, but it's quite powerful which is why, even after, I don't know, 30 years of the printf() statement, the same specifiers are used.