Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

There is no way to format variables for display only #78

Open
ghost opened this issue Aug 15, 2021 · 1 comment
Open

There is no way to format variables for display only #78

ghost opened this issue Aug 15, 2021 · 1 comment

Comments

@ghost
Copy link

ghost commented Aug 15, 2021

If a variable is a float and set to a value that is the result of a math expression, the result can be an unfriendly value for display.
"Your store discount was $1.34254535" as opposed to "Your store discount was $1.34".
The runner.js code is not implemented in a way where it can distinguish between a variable being displayed or used in an expression.

The YS docs have this to say, which is not how things work in bondage.js and not good enough imo.

Numbers are always decimals (technically, floating point) regardless of what values you give them. This means if you give a variable the value of 1 when you get it back out from Yarn Spinner it will be 1.0.

This custom formatting should be part of the core engine in my opinion, but if that is asking too much, then the solution below would be a good trade-off, so devs have a way to implement variable formatting themselves.

A possible solution would be to extend the variable storage class to have another function called display, which would get the variable when it is being displayed. The runner would be refactored to call display() when appropriate. This would allow custom implementations of the variable storage class to handle custom formatting of variables for display purposes.

You may be wondering how a variable's formatting would be specified. This could be accomplished by implementing a custom variable model for your custom varuable storage class that isn't simply a variable, but instead a class with two properties, value and format. A custom command setvarformat could be added for setting the format for a variable. When display is called the function could check to see if a format is specified and then do the formatting or not, if none is specified.

@ghost
Copy link
Author

ghost commented Aug 15, 2021

Here is an easier work around. Create a custom command that formats the variable and keep two copies of the variable, one for calculations and one for display. After setting the non-display variable, call the custom command to format the display variable. This is necessary when max precision is required for the calculations and not for displayed value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants