Weird NumberFormatter behavior in Flex, AS3 - actionscript-3

I came across this weird behavior while using NumberFormatter in AS3.
It's important to understand that I need to use NumberBaseRoundType.UP because I need to use NumberBaseRoundType.DOWN when applying a credit.
Considering the rounding precision that has a value of 2, I guess the NumberFormatter shouldn't change my number. In the example below, 2.43 once formatted become 2.44..!
So the Math.pow(...) is not the solution I'm looking for PLUS, I am very interested to understand what and why this is happening. THANKS!
var roundUp:NumberFormatter = new NumberFormatter();
roundUp.rounding = NumberBaseRoundType.UP;
roundUp.precision = 2;
trace(roundUp.format(2.41)); // Output : 2.41
trace(roundUp.format(2.42)); // Output : 2.42
trace(roundUp.format(2.43)); // Output : 2.44 <-- ???
trace(roundUp.format(2.44)); // Output : 2.44
trace(roundUp.format(2.45)); // Output : 2.46 <-- ???
trace(roundUp.format(2.46)); // Output : 2.46

This is not a complete answer. But to start off with which NumberFormatter class are you using (what's the package it comes from). Some of the source is available, some of it is contained in the player itself and isn't accessible, but in looking at the on included in Spark from the 4.6 framework it includes this class level comment that gives some insight:
/**
* The NumberFormatter class provides locale-sensitive formatting
* and parsing of numeric values. It can format <code>int</code>,
* <code>uint</code>, and <code>Number</code> objects.
*
* <p>This class is a wrapper class around the
* flash.globalization.NumberFormatter class.
* Therefore, the locale-specific formatting
* is provided by the flash.globalization.NumberFormatter.
* However, this NumberFormatter class can be used in MXML declarations,
* uses the locale style for the requested Locale ID name, and has
* methods and properties that are bindable.
* </p>
*
* <p>The flash.globalization.NumberFormatter class use the
* underlying operating system for the formatting functionality and
* to supply the locale-specific data. On some operating systems, the
* flash.globalization classes are unsupported, on these systems this wrapper
* class provides fallback functionality.</p>
*
* #mxml <p>The <code><s:NumberFormatter></code> tag inherits all of the tag
* attributes of its superclass and adds the following tag attributes:</p>
*
* <pre>
* <s:NumberFormatter
* <strong>Properties</strong>
* negativeNumberFormat="<i>locale and OS dependent</i>"
* />
* </pre>
*
* #includeExample examples/NumberFormatterExample1.mxml
* #includeExample examples/NumberFormatterExample2.mxml
*
* #see flash.globalization.NumberFormatter
*
* #langversion 3.0
* #playerversion Flash 10.1
* #playerversion AIR 2.5
* #productversion Flex 4.5
*/
Ultimately though it states that the player uses the underlying OS in some way to achieve the formatting. It is certainly a strange problem but I'd be surprised if this wasn't ever caught before and an explanation hasn't been posted or at least a bug report if it is in fact an issue in flash player. Some more details on your SDK version and player version may help, also have you tried fiddling with either of these is there any change in results?
Also depending on what you're attempting to achieve you may be able to just get around the issue by writing your own class to deal with doing the formatting for this case if it's just a one off scenario that you need to deal with this, if it's more of a system wide used thing that needs more of the functionality of the NumberFormatter class I could understand wanting to resolve the underlying issue.

Related

Yii2 override core class - class not found

I try to extend BaseHtml class by follow this tutorial
But is not find the BaseHtml extended class
namespace common\components;
class Html extends \yii\helpers\BaseHtml {
}
this is the error:
Class 'yii\helpers\BaseHtml' not found
Where I am wrong?
Update
I try to copied the Html.php file locate in Helpers and is still the same
<?php
/**
* #link http://www.yiiframework.com/
* #copyright Copyright (c) 2008 Yii Software LLC
* #license http://www.yiiframework.com/license/
*/
namespace yii\helpers;
/**
* Html provides a set of static methods for generating commonly used HTML tags.
*
* Nearly all of the methods in this class allow setting additional html attributes for the html
* tags they generate. You can specify, for example, `class`, `style` or `id` for an html element
* using the `$options` parameter. See the documentation of the [[tag()]] method for more details.
*
* For more details and usage information on Html, see the [guide article on html helpers](guide:helper-html).
*
* #author Qiang Xue <qiang.xue#gmail.com>
* #since 2.0
*/
class Html extends BaseHtml
{
}
And now I have another problem. It not find BaseHtml fro the original class
vendor/yiisoft/yii2/helpers/Html.php
Solution found
I refactored the override file and phpStorm change me the path to BaseHtml in vendor/yiisoft/yii2/classe.php
BaseHtml.php should be under /vendor/yiisoft/yii2/helpers/ folder.
If it's not there, you will need to update your vendors or try to copy file BaseHtml.php to your yii2/helpers at yii2 repo: https://github.com/yiisoft/yii2/blob/b7b9c8ba40d65ec7c745ee639a8a6eb96e7aea75/framework/helpers/BaseHtml.php
Hope it helps.

Android ListView binding programmatically

There are many examples of doing this in axml, but I would like to have a complete binding using code behind. To be honest, I would like to have NO axml, but seems like creating all the controls programmatically is a nightmare.
I first tried the suggestions at:
MvxListView create binding for template layout from code
I have my list binding from code-behind, and I get six rows (so source binding is working); but the cells itself does not bind.
Then at the following url:
Odd issue with MvvmCross, MvxListViewItem on Android
Stuart has the following comment: Have looked through. In this case, I don't think you want to use DelayBind. DelayBind is used to delay the binding action until next time the DataContext is set. In Android's MvxAdapter/MvxListItemView case, the DataContext is passed in the ctor - so DataContext isn't set again until the cell is reused. (This is different to iOS MvxTableDataSource).
So in essence, the only example I see shows DelayBind, which shouldn't work.
Can someone please show me some examples... thanks in advance.
Added reply to Comments:
Cheesebaron, first of all, a huge thank you and respect for all your contributions;
Now, why not use axml? Well, as programmers, we all have our own preferences and way of doing stuff - I guess I am old school where we didn't have any gui designer (not really true).
Real reasons:
Common Style: I have a setup where Core has all the style details, including what all the colors would be. My idea is, each platform would get the style details from core and update accordingly. It's easy for me to create controls with the correct style this way.
Copy-Paste across platform (which then I can even have as linked files if I wanted). For example, I have a login screen with web-like verification, where a red error text appears under a control; overall on that screen I have around 10 items that needs binding. I have already got iOS version working - so starting on Droid, I copied the whole binding section from ios, and it worked perfectly. So, the whole binding, I can make it same across all platform... Any possible error in my way will stop at building, which I think is a major advantage over axml binding. Even the control creation is extremely similar, where I have helpers with same method name.
Ofcourse I understand all the additional layout that has to be handled; to be honest, it's not that bad if one really think it through; I have created a StackPanel for Droid which is based on WP - that internally handles all the layouts for child views; so for LinearLayout, all I do is setup some custom parameters, and let my panel deal with it. Relative is a different story; so far, I have only one screen that's relative, and I can even make it Linear to reduce my additional layout code.
So, from my humble point of view, for my style, code-behind creation allows me to completely copy all my bindings (I do have some custom binding factories to allow that), copy all my control create lines; then only adding those controls to the view is the only part that is different (then again, droid and WP are almost identical). So there is no way I can miss something on one platform and all are forced to be the same. It also allows me to change all the styles for every platform just by changing the core. Finally, any binding error is detected during compile - and I love that.
My original question wasn't about NOT using axml... it was on how to use MvxListView where all the binding is done in code-behind; as I have explained, I got the list binding, but not the item/cell binding working.
Thanks again in advance.
Here is part of my LoginScreen from droid; I think it's acceptable amount of code for being without axml file.
//======================================================================================================
// create and add all controls
//======================================================================================================
var usernameEntry = ControlHelper.GetUITextFieldCustom(this, "Username.", maxLength: 20);
var usernameError = AddErrorLabel<UserAuthorization, string>(vm => ViewModel.Authorization.Username);
var passwordEntry = ControlHelper.GetUITextFieldCustom(this, "Password.", maxLength: 40, secureTextEntry: true);
var passwordError = AddErrorLabel<UserAuthorization, string>(vm => ViewModel.Authorization.Password);
var loginButton = ControlHelper.GetUIButtonMain(this);
var rememberMe = new UISwitch(this);
var joinLink = ControlHelper.GetUIButtonHyperLink(this, textAlignment: UITextAlignment.Center);
var copyRightText = ControlHelper.GetUILabel(this, textAlignment: UITextAlignment.Center);
var copyRightSite = ControlHelper.GetUIButtonHyperLink(this, textAlignment: UITextAlignment.Center);
var layout = new StackPanel(this, Orientation.Vertical)
{
Spacing = 15,
SubViews = new View[]
{
ControlHelper.GetUIImageView(this, Resource.Drawable.logo),
usernameEntry,
usernameError,
passwordEntry,
passwordError,
loginButton,
rememberMe,
joinLink,
ControlHelper.GetSpacer(this, ViewGroup.LayoutParams.MatchParent, weight: 2),
copyRightText,
copyRightSite
}
};
I just came across a similar situation myself using Mvx4.
The first link you mentioned had it almost correct AND when you combine it from Staurts comment in the second link and just remove the surrounding DelayBind call, everything should work out ok -
public class CustomListItemView
: MvxListItemView
{
public MvxListItemView(Context context,
IMvxLayoutInflater layoutInflater,
object dataContext,
int templateId)
: base(context, layoutInflater, dataContext, templateId)
{
var control = this.FindViewById<TextView>(Resource.Id.list_complex_title);
var set = this.CreateBindingSet<CustomListViewItem, YourThing>();
set.Bind(control).To(vm => vm.Title);
set.Apply();
}
}
p.s. I have asked for an Edit to the original link to help others.

AS3 Object indexing causing Flixel misbehaviour--what's wrong with this code?

I'm getting my feet wet with AS3, Flixel and component/entity systems (yes, all at the same time), and my entities (which subclass FlxSprite) aren't being added correctly (i.e., not at all). I've just spent a good two hours nailing down the offending line. If I remove it, the rest of the game chugs along happily.
What's wrong with this code?
public function addComponent(c:Component):void
{
var type:String = Object(c).constructor.toString();
FlxG.log("type=" + type);
this._components[type] = c; // The evil line
FlxG.log("now type=" + _components[type]);
c.setData(this);
}
components is an Object field being used as a map/dictionary. type gets set to [class PlayerComponent]. Based on googling, this is valid and should work as intended.
Based on the output from the console, it's just bailing after that line--not crashing entirely. What's going on? More details gladly offered upon request.
I'm not certain about Component - not my forte - but I do know that FlxGroup and its children (which include FlxState) have a method called add() which adds children to them.
So if you have an FlxSprite, the correct way (in flixel) to add it to the chain of things to update/draw is to use that; you can add it directly to your state or to a group that is a child of the state.
Function docs: http://flixel.org/docs/org/flixel/FlxGroup.html#add()

What is a style protochain?

I keep encountering many references to style protochain in the documentation about css in flex. I tried to look it up but I couldn't find any relevant information. Can anyone tell me what does the class StyleProtochain do?
In its source code, there is the following comment.
/**
* #private
* This is an all-static class with methods for building the protochains
* that Flex uses to look up CSS style properties.
*/
Cool, it tells me that there are a couple of methods for building the protochains, now I am supposed to know what it does, but I have no idea what a protochain is.
CSS styles are inherited. This means that when you create a new object and place it as a child of another in the DOM, this new object will have to inherit all the "inheritable" styles from the parent. Then it will override these styles through its stylename or inline styles.
The StyleProtoChain class is responsible for building this style list of any object ( which can have a style ) that is created. It is named thus because this class has to go up the DOMtree ( like going up the prototype chain ... perhaps a misnomer!) and construct the list of styles for this object. The order in which this goes up is specified by this comment.
/**
* #private
* If the styleName property points to a UIComponent, then we search
* for stylable properties in the following order:
*
* 1) Look for inline styles on this object
* 2) Look for inline styles on the styleName object
* 3) Look for class selectors on the styleName object
* 4) Look for type selectors on the styleName object
* 5) Look for type selectors on this object
* 6) Follow the usual search path for the styleName object
*
* If this object doesn't have any type selectors, then the
* search path can be simplified to two steps:
*
* 1) Look for inline styles on this object
* 2) Follow the usual search path for the styleName object
*/
Hope this helps. If anyone wants further reference, you can find a link to the source here

Any difference between .innerHTML and .set('html','') in mootools?

To set the html of elements on my site, I use mostly
$('elementId').innerHTML = "<p>text</p>";
Looking through the mootools docs, I found this example given:
$('myElement').set('html', '<div></div><p></p>');
Is there any difference between these? Should I go through and change .innerHTML to the mootools method, or doesn't it make a difference?
the reason why the first one works is because - as it stands - a $ selector (document.id) in mootools returns the actual element. this - in normal browsers - is identical to document.getElementById() and the element object exposes any and all of its attributes/properties for you to edit.
the problems with NOT using .set are:
when mootools 2.0 aka MILK gets released, it won't work as it will be wrapped like jQuery and the selector won't return the object (mootools is becoming AMD hence it won't modify native Types - Element, Array, Number, String, Function(maybe!) - prototypes).
you cannot chain this. with set you can: $('someid').set("html", "loading...").highlight();, for example.
set is overloaded - it can set either a single property or multiples by means of passing an object. eg, element.set({html: "hello", href: "#", events: boundObj});
look at https://github.com/mootools/mootools-core/blob/master/Source/Element/Element.js#L936-942 - you can pass an array as an argument and it will join it for you, this makes it easy to work with multi-line strings and ensures performance in IE
edit: the BBT fan has kind of opened a separate topic: should the framework try to block you / prevent you from doing things that break the browser?
if you want to, you can add disallowed elements by changing that setter Element.Properties.html.set = function() { var tag = this.get("tag"); ... check tag }; - isn't mootools great?
mootools - by default - will NOT try to prevent you from doing stupid shit [tm] - that's your responsibility :) try setting height on an element to a negative value in IE, for example. should the Fx class prevent you from doing that? No. Should the setter prevent you? No. The footprint of constant checks to see if you are not breaking means it will slow everything down in performance-critical cases like animations.