@numerical-type: bind

How can the numerical type of a node that can contain different units be changed using
@numerical-type: bind
See below, The node '[‘prijs/bewerking’ and ‘aantal’ depends on node ‘eenheid’ and can be in pieces, meter or square meters

	'type bewerking': text  -> ^ ^ >'model'.'Bewerkingen'[]
	'eenheid': text //per meter, m^2 or stuks
	'prijs/bewerking': number 'euro' = >'type bewerking'.'prijs'
	'aantal': number 'aantal'
	'Prijs': number 'euro'= from 'euro' product ( .'aantal' as 'aantal' , .'prijs/bewerking' )

This is an example of how you can use bind for a user provided numerical type and number of decimals in the webclient:

root {
	'Units': collection ['Unit'] {
		'Unit': text
		'Decimals': number 'count'
	}
	'Item': collection ['K'] {
		'K': text
		'Unit': text -> ^ .'Units'[]
		'Amount': number 'unit'
			@numerical-type: >'Unit' // e.g. m^2 or meters or pieces
	}
}

numerical-types
	'count'
	'unit' @numerical-type: bind .'Units'* as $ (
		label: $ .'Unit'
		decimals: $ .'Decimals'
	)

This way, the webclient will show whatever value the user provides for the Unit as the numerical type for Amount.

I have a following-up question regarding the bind function:

My numerical-type looks as follows:

root {
	'Currencies': collection ['Currency'] {
		'Currency': text
		'Sign': text
		'Decimals': number 'count'
     }

	'Currency': text -> .'Currencies'[] = "EUR"
	'Sales Price': number 'cent/Kg'
		@numerical-type: ^ >'Currency'
}

numerical-types
    'count'
	'cent/Kg'
		= '10^-3 €cent' / '10^-3 kg'
		@numerical-type: bind .'Currencies'* as $ (
			label: concat ( $ .'Sign', "/Kg" ) // <- equal to €
			decimals: $ .'Decimals' // <- set to 2
		)

Whenever ‘Currency’ is equal to “EUR”, label contains €/Kg and decimal points are qual to 2, creating, for example, 2,34 €/Kg after saving.
However, the problem lies in inserting 2,34 when adding a new value. It only allows me to insert 234, which would translate to 2,34 after saving.

How to solve this issue? How do I enable myself to insert 2,34 without being forced to remove my bind function?

Are you using the correct decimal separator? It depends on your OS and/or browser settings. Please try using the dot (.) as the separator.

1 Like

Yes, in both cases, inserting 2,34 and 2.34, the value jumps to 2, which translates to 0,02 after saving.

	'cent/Kg'
		= '10^-3 €cent' / '10^-3 kg'
		@numerical-type: bind .'Relationship Management'.'Currencies'* as $ (
			label: concat ( $ .'Sign', "/Kg" )
			// decimals: $ .'Decimals'
			decimals: 2

I thought the above would make the decimals static, but the problem still occurs. Only after saving the newly added modifications, one is allowed to put decimal points.

I tested the following numerical value in case the definition of ‘cent/Kg’ caused it.

'test'
	@numerical-type: bind .'Relationship Management'.'Currencies'* as $ (
		label: concat ( $ .'Sign', "/Kg" )
		// decimals: $ .'Decimals'
		decimals: 2
	)

This still causes, for example, input of 22,34 to jump to 22, which becomes 0,22 $/Kg after saving.

I have asked the webclient team to investigate this. I’m not sure if the decimals property of a numerical type can be changed dynamically while creating a new entry or editing an existing one and switching the currency, for example.

This should work, using a @default: "EUR" annotation on the 'Currency' reference:

root {
	'Currencies': collection ['Currency'] {
		'Currency': text
		'Sign': text
		'Decimals': number 'count'
	}
	'Entries': collection ['ID'] {
		'ID': text
		'Currency': text -> ^ .'Currencies'[] @default: "EUR"
		'Sales Price': number 'cent/Kg' @numerical-type: >'Currency'
	}
}

numerical-types
	'count'
	'cent/Kg'
		@numerical-type: bind .'Currencies'* as $ (
			label: concat ( $ .'Sign', "/Kg" ) // <- equal to €
			decimals: $ .'Decimals' // <- set to 2
		)