cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Try the Materials Informatics Toolkit, which is designed to easily handle SMILES data. This and other helpful add-ins are available in the JMP® Marketplace
Choose Language Hide Translation Bar
swanthog
Level II

Convert to nearest whole number - No Rounding

Hi.

 

Being new to JMP, I'm sure this is a typical beginner's mistake.

 

I need to convert a decimal number to a whole number without rounding.

 

Example 1: The value 0.5986 is copied from column1 and saved to column2. 0 should be saved to column2. Instead 1 is being saved.

Example 2: The value 1.5986 is copied from column1 and saved to column2. 1 should be saved to column2. Instead 2 is being saved.

 

I've tried using Round(x, 0), but the number is always rounded up.

I've also tried formatting the value from column1,  and converting back to a number (fails).

 

Column1 is defined: 

New Column( "Time",
		Numeric,
		Continuous,
		Format( Best, 12),
	);

Column2 is defined:

New Column( "WholeSeconds",
		Numeric,
		Continuous,
		Format( Fixed Dec, 5, 0),
	);

Code:

For Each Row( 
	lvTime = dt:Time;
	dt:WholeSeconds = Round( lvTime,0);
);

Any help is greatly appreciated.

 

Thank you.

Lawrence
1 ACCEPTED SOLUTION

Accepted Solutions
Georg
Level VII

Re: Convert to nearest whole number - No Rounding

In addition to @txnelson , here's the code for it:

As you can see, you can uses a formula, or written values to that column.
It's better to avoid loops (for each row), if tables can do it without, if possible. 

Names Default To Here( 1 );

cdt = New Table( "Example Floor",
	add rows( 2 ),
	New Column( "Time", Numeric, Continuous, Format( Best, 12 ), set values( [0.5986, 1.5986] ) ),
	New Column( "WholeSeconds_formula", Numeric, Continuous, Format( Best, 12 ), Formula( Floor( :Time ) ) ),
	New Column( "WholeSeconds_value", Numeric, Continuous, Format( Best, 12 ), set each value( Floor( :Time ) ) )
);

Georg_0-1614420933166.png

 

Georg

View solution in original post

4 REPLIES 4
txnelson
Super User

Re: Convert to nearest whole number - No Rounding

You can use Floor() and Ceiling() functions.

Jim
Georg
Level VII

Re: Convert to nearest whole number - No Rounding

In addition to @txnelson , here's the code for it:

As you can see, you can uses a formula, or written values to that column.
It's better to avoid loops (for each row), if tables can do it without, if possible. 

Names Default To Here( 1 );

cdt = New Table( "Example Floor",
	add rows( 2 ),
	New Column( "Time", Numeric, Continuous, Format( Best, 12 ), set values( [0.5986, 1.5986] ) ),
	New Column( "WholeSeconds_formula", Numeric, Continuous, Format( Best, 12 ), Formula( Floor( :Time ) ) ),
	New Column( "WholeSeconds_value", Numeric, Continuous, Format( Best, 12 ), set each value( Floor( :Time ) ) )
);

Georg_0-1614420933166.png

 

Georg
swanthog
Level II

Re: Convert to nearest whole number - No Rounding

Thank you, Georg, and txnelson.

 

I appreciate the name resolution tip too. I hadn't come across it yet in the documentation.

 

I'm guessing the recommendation for avoiding loops is due to negative performance impact. Is that right? What (roughly) is the impact/difference to performance using loops vs table formulas?

 

Thanks!

Lawrence

Lawrence
Georg
Level VII

Re: Convert to nearest whole number - No Rounding

Dear @swanthog, in a script I tested out three different methods for calculating a square y = x*x in a table with different number of rows.

The outcome is,

  • that the loop takes ten times more time
  • as the table operation
  • and the matrix calculation is 5x faster than the table operation

Also in terms of clean code and less errors, it is in my opinion a good recommendation to avoid loops, if there are other methods (table and matrix), that have built in functionality to adress each element.

 

But for small scripts, and if performance is no issue, loops are also ok. Also readability (to the eyes of the writer) counts.

 

Georg_0-1614669857895.png

 

Georg