1

I am converting the following string to a number, but it is removing the "+" character. My intent is to have the following output:

What I am currently getting back:

"change": "+0.01",

My desired output:

"change": +0.01,

Using "Number()" with remove the "+"

How do I pull this off?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
user992731
  • 3,400
  • 9
  • 53
  • 83
  • 1
    In simple words, You cannot achieve this. `+0.01` is a string, not a number – Agam Banga May 06 '17 at 14:43
  • _"My desired output"_ Is that text? A part of an object? Some JSON? What? Be clear. Ideally present your [MCVE]; you've been here for five and a half years, and asked 87 questions, so you should know by now what we expect. – Lightness Races in Orbit May 06 '17 at 14:45
  • @Jean-BaptisteYunès: Don't provide solutions in the comments section, as we cannot peer review them properly. For example, I cannot downvote your wrong comment; that suggestion will result in a string, which the OP stated they are trying to get away from. – Lightness Races in Orbit May 06 '17 at 14:47

3 Answers3

2

A number is a number is a number. Not a string. Not a representation of a number. But a number. There is no "+" character or "-" character stored with it. There are no commas, or periods, or other characters. It is a number. You can't have "plus symbol two apples"; you just have two apples.

The number 0.01 is positive. It is irrelevant where that number came from, or what the original string looked like that was used to produce the number.

If you wish to stringise a positive number with a leading +, then that is about how you deal with the Number object later when you later "convert" it back to a string. You can achieve that simply with an if statement.

However to me your "desired outcome" looks like an object declaration, in which the presence or not of a leading + would again be irrelevant, as you are storing a number. You can format it however you like at the receiving end of the data, if you need to present it to a human.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

You can't. +0.01 is equal to 0.01. If you want to keep + then you need it to be string.

If you want to get number from that string and check if it has + sign then use RegEx or split that string.

Oen44
  • 3,156
  • 1
  • 22
  • 31
0

A + prefix for numbers would be a terrible idea in JavaScript, that is why it is not allowed.

In this language, + is already used for many things like:

  • String concatenation (e.g. 'foo' + 'bar')
  • Addition of numbers (e.g. 1 + 2)
  • Variable increment (e.g. i++)
  • Explicit type conversion with the unary operator (e.g. +'0.07')
  • Implicit type conversion (coercion) when operands are not of the same type (e.g. 4 + '6')

This is an error-prone part of JavaScript because you may have code like this:

var i = 2;

console.log(+'+1' + '3' + ++i + +'7');

There are two exceptions, however:

  1. +0, which is not the same thing as -0 (see Object.is(+0, -0))
  2. Exponential numbers (see Object.is(1e+2, 100))

In your case, Number('+0.01') or parseFloat('+0.01') will return 0.01. This is the expected (and reasonable) behavior.

Badacadabra
  • 8,043
  • 7
  • 28
  • 49