0

Is there something i can do here

    const ecommerce = {
    purchase: {
        actionField: {
        },
    },
}
2
  • Is it possible that the price as been entered with something else than zeros after the dot ? Like 8.23 ? Commented Feb 15, 2018 at 12:22
  • no, Theres like 3 Prices that have been entered with zeroes behind, all others are single digit integers Commented Feb 15, 2018 at 12:26

4 Answers 4

1

As it looks like, the inputted value is a string. You could convert the string to number by using an unary plus + in front of the variable.

name: "PRICEPLAN-" + +price,

An other way would include some sanity checks and urges the user to input a valid value.

Sign up to request clarification or add additional context in comments.

Comments

1

@Nina Scholz' answer is correct and concise. The other way in JavaScript to do this, and which I personally prefer because it's more semantic, is to use Number().

name: "PRICEPLAN-" + Number(price),

I find that good semantics make it easier for others to understand my code (and for me too, when I come back to it 6 months later.)

As others have pointed out, this will not coerce your values to an integer, so you will want to be sure about your inputs.

Comments

0

If you want Integer value, then you can use var parseInt() function in JS

var a = parseInt("10.00")

will convert it to "10"

radix : An integer between 2 and 36 that represents the radix (the base in mathematical numeral systems) of the above mentioned string. Specify 10 for the decimal numeral system commonly used by humans. Always specify this parameter to eliminate reader confusion and to guarantee predictable behavior.

After adding radix it will assure the output will be decimal :

var a = parseInt("010.00", 10)

4 Comments

Thank you, thats exactly the function i was looking for!
You are welcome! Please accept this answer, if you are satisfied with answer. :)
you may add a radix, because you could get wrong results, like with '011' you get 9, because the leading zero is read as octal system.
@SachinBankar so with a radix it would look something like this? name: "PRICEPLAN-" + parseInt(price, 10),?
0

To be sure to have an Interger, use parseInt(). As Number() won't prevent numbers like 3.11 to be transformed to Integers.

name: "PRICEPLAN-" + parseInt(price),

And using ES6 template notation:

name: `PRICEPLAN-${parseInt(price)}`,

1 Comment

If @Chris R's answer meets your needs, you should accept it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.