Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Possible to save type in object?

The below example gives the correct calculation, but for some reason p.calcPrice is a string. I would expect that it is a number, as .toFixed() doesn’t make sense in a string.

Whenever I use p.calcPrice, do I then have to cast it with +p.calcPrice, or can I have TypeScript treat as a number, so I don’t have to cast it?

p.calcPrice = (
  price *
  +p.amount *
  (1 - pD / 100) *
  (1 - mD / 100)
).toFixed(2);

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

toFixed returns a string, you have to cast it manually.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Number/toFixed#return_value

one way for casting would be to wrap it in Number:

p.calcPrice = Number((price * +p.amount * (1 - pD / 100) * (1 - mD / 100)).toFixed(2));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading