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

numberToString - return string directly #22300

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions cs/ccxt/base/Exchange.Number.cs
Expand Up @@ -300,6 +300,8 @@ public static string NumberToString(object number)
{
if (number == null)
return null;
if (number.GetType() == typeof(string))
return number;
if (number.GetType() == typeof(Int32) || number.GetType() == typeof(Int64))
return number.ToString();

Expand Down
2 changes: 2 additions & 0 deletions python/ccxt/base/decimal_to_precision.py
Expand Up @@ -169,6 +169,8 @@ def number_to_string(x):
# avoids scientific notation for too large and too small numbers
if x is None:
return None
if isinstance(x, str):
return x
d = decimal.Decimal(str(x))
formatted = '{:f}'.format(d)
return formatted.rstrip('0').rstrip('.') if '.' in formatted else formatted
1 change: 1 addition & 0 deletions ts/src/base/functions/number.ts
Expand Up @@ -39,6 +39,7 @@ const precisionConstants = {

function numberToString (x) { // avoids scientific notation for too large and too small numbers
if (x === undefined) return undefined;
if (typeof x === 'string') return x;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unnecessary and actually less efficient, seeing as it does a string comparison https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/toString#description

The String object overrides the toString method of Object; it does not inherit Object.prototype.toString(). For String values, the toString method returns the string itself (if it's a primitive) or the string that the String object wraps. It has the exact same implementation as String.prototype.valueOf().

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure it's less efficient, you can test out here: https://jsbench.me/91lvmdl2e3/1
also tried other testers, everywhere this approach wins

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@carlosmiei should we close this PR? is numberToString always applied against numerics for sure? does it anywhere depend on users ex.number setting?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this approach would be a gain, because the toString method of a string just returns the string itself, so having another conditional just seems like extra, but this is such a minor thing that it doesn't really matter

if (typeof x !== 'number') return x.toString ();
const s = x.toString ();
if (Math.abs (x) < 1.0) {
Expand Down