Lasso Soft Inc. > Home

[formatted_decimal]

Linkformatted_decimal
AuthorKyle Jessup
CategoryMath
Version9.x
LicensePublic Domain
Posted07 Oct 2013
Updated07 Oct 2013
More by this author...

Description

This object attempts to work like a decimal, but is able to store its formatting flags, much in the manner of LP8.
 
Most mathmatical operators are supported against both integers and decimals.
 
Note that this object type does not inherit from decimal.

Sample Usage

local(one = formatted_decimal(28.456789001))

#one->setFormat(-precision=2)

#one->asString // resulting in '28.46'

Source Code

Click the "Download" button below to retrieve a copy of this tag, including the complete documentation and sample usage shown on this page. Place the downloaded ".inc" file in your LassoStartup folder, restart Lasso, and you can begin using this tag immediately.

define formatted_decimal => type {
	data public value::decimal,
		private flags
	
	public onCreate(flags = (:)) => { 
		.value = 0.0
		.flags = #flags
	}
	public onCreate(v::decimal, flags = (:)) => { 
		.value = #v
		.flags = #flags
	}
	public onCreate(v, flags = (:)) => { 
		.value = decimal(#v)
		.flags = #flags
	}

	public setFormat(...rest) => {
		.flags = #rest
	}
	
	public asString() => .value->asString(:.flags or (:))
	
	public +(rhs::formatted_decimal)::formatted_decimal => formatted_decimal(.value + #rhs->value, .flags)
	public +(rhs::decimal)::formatted_decimal => formatted_decimal(.value + #rhs, .flags)
	public +(rhs::integer)::formatted_decimal => formatted_decimal(.value + #rhs, .flags)
	
	public -(rhs::formatted_decimal)::formatted_decimal => formatted_decimal(.value - #rhs->value, .flags)
	public -(rhs::decimal)::formatted_decimal => formatted_decimal(.value - #rhs, .flags)
	public -(rhs::integer)::formatted_decimal => formatted_decimal(.value - #rhs, .flags)
	
	public /(rhs::formatted_decimal)::formatted_decimal => formatted_decimal(.value / #rhs->value, .flags)
	public /(rhs::decimal)::formatted_decimal => formatted_decimal(.value / #rhs, .flags)
	public /(rhs::integer)::formatted_decimal => formatted_decimal(.value / #rhs, .flags)
	
	public *(rhs::formatted_decimal)::formatted_decimal => formatted_decimal(.value * #rhs->value, .flags)
	public *(rhs::decimal)::formatted_decimal => formatted_decimal(.value * #rhs, .flags)
	public *(rhs::integer)::formatted_decimal => formatted_decimal(.value * #rhs, .flags)
	
	public onCompare(rhs)::integer => .value->onCompare(#rhs)
}

define decimal->/(rhs::formatted_decimal) => formatted_decimal(self / #rhs->value)
define integer->*(rhs::formatted_decimal) => self * #rhs->value

Comments

No comments

Please log in to comment

Subscribe to the LassoTalk mail list

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft