-1

In JavaScript, is it possible to "lock" a decimal number, to preserve "floating point" numbers that end with zeroes?

Example, I have 2 different numbers, like this: (pseudo code)

let a = 1.0;
let b = 1.00;
a === b  // true, should be false, because different decimals.
print(a) // "1", should be "1.0"
print(b) // "1", should be "1.00"

(should also be different from a "true int" 1)

I want them to identify as different from each other, because they have different amount of zeroes at the end.

But stupidly "efficient" JavaScript rounds them both down to "integer" 1 & therefore they are equal.

I am aware of the Number.toFixed(Number) & Number.toPrecision(Number) functions, but to use them like this I have to first calculate the length of the number, which I can't because JavaScript have already rounded away the zeroes.

I have also been thinking of "cutting" off the number at the decimal point & store them in an array... but again, rounded to an "int" without a decimal point.

(Yes, I know that the concept of "float" doesn't exist in JS, I use it here to diferentiate between numbers with or without decimals).

Thanks for any help.

Sebastian Norr
  • 3,075
  • 2
  • 8
  • 15
  • 1
    Why not keep it as string? – Rehban Khatri Nov 14 '19 at 13:18
  • Because: "1.0" + "1.00" = "1.01.00" which make no sense. – Sebastian Norr Nov 14 '19 at 13:19
  • 2
    So if it's an integer, what is 1.0+1.00? 2.0 or 2.00 – Rehban Khatri Nov 14 '19 at 13:20
  • 2
    Why should javascript keep the leading zeros? They're just on/off bit inside a calculator, there's not a concept of base10 length. What you see is just a human-friendly representation, so your question makes no sense – Cristian Traìna Nov 14 '19 at 13:21
  • `1.00 === 1`. There is no way to represent 1.00 in a different way than 1 in the [IEE754 standard](https://en.wikipedia.org/wiki/IEEE_754). If you want display a number in a certain way, you need to convert it to a string. Related: [Is floating point math broken?](https://stackoverflow.com/questions/588004) – adiga Nov 14 '19 at 13:31
  • @adiga The problem is that: ```1.00.toString() === "1";``` So how do I make a "decimal preserved string" out of a decimal number ending in zeroes? – Sebastian Norr Nov 14 '19 at 13:32
  • You can't. When `toString` is called on the value, it is just 1. Internally, 1, 1.0, 1.00 they are all represented as `1.f * 2^(e−1023)` where f = 0 and e = 1023 – adiga Nov 14 '19 at 13:40
  • @adiga No, this post & [this](https://stackoverflow.com/questions/2283566/how-can-i-round-a-number-in-javascript-tofixed-returns-a-string) is not remotely equal. As I said. I'm aware of the ```.typeOf()``` function & it doesn't work unless there is a way to find out how long the number is before the rounding happens. – Sebastian Norr Nov 14 '19 at 13:47
  • 1
    @SebastianNorr the number has not length, and there is no rounding happening. `var a = 1.00, b = 0x01;`and even `var c = 6/(2*3);` are the exact same value. Just because you wrote them differently doesn't change the number. If you want some sort of format, you need to take care of that yourself; numbers simply don't contain it. – Thomas Nov 14 '19 at 14:10

1 Answers1

0

To compare 2 variables of indefinite type, the variables must be cast internally. This is the problem. Because both A and B are a set with the thickness 1. Therefore the result is True.

However, if you want to compare the number of zeros, you have to compare them as a string.

So either you declare the variables with

let a = '1.0';
let b = '1.00';

or you cast the variables using

a.toString() === b.toString();
MThiele
  • 384
  • 1
  • 9
  • That could maybe work. I could store them as strings & compare the strings & I could use Number.paseInt(Number) when I need to do math on them. – Sebastian Norr Nov 14 '19 at 13:26
  • The problem is that: 1.00.toString() = "1"; So how do I make a "decimal preserved string" out of a decimal number ending in zeroes? – Sebastian Norr Nov 14 '19 at 13:30