0

I was asked this question in a job interview the other day. And I thought "This will be easy I'll just use parseFloat()." And then the interviewer said I was not allowed to use parseFloat(). That I had to manually make the conversion myself without using a function to do the casting for me.

So if you know how to convert the decimal string "875.43", into a number in JavaScript without using parseFloat, please show me.

Thank you, CM

Chris Mazzochi
  • 179
  • 1
  • 15
  • related http://stackoverflow.com/questions/1133770/how-do-i-convert-a-string-into-an-integer-in-javascript – dippas Apr 15 '17 at 02:04
  • 1
    This link is not helpful. I said I could out use functions such as parseFloat or parseInt to do the casting for me. – Chris Mazzochi Apr 15 '17 at 02:11
  • were they asking to do a digit by digit conversion by splitting the string and manipulating each digit? A really easy way, if you have a `var myStr = "2345.65";` in code is to do this: `var myInt = +myStr;` Or even "var myInt = +"2345.65";`. `typeof` returns "number" for that? – Ken H. Apr 15 '17 at 02:13
  • What you've begun to describe sounds a little like what the interviewer described. – Chris Mazzochi Apr 15 '17 at 02:17
  • that answers my question above... Sounds like the interviewer basically wanted some mathematical calculation to take the powers of 10, multiply each digit by the appropriate power, and sum the values. Probably just wanted to get some insight into your familiarity with different ways to solve the problem. Or see how you think on your feet. As someone pointed out below, really not much of a point to doing it other than make something more difficult to do. – Ken H. Apr 15 '17 at 02:19

4 Answers4

0

ECMA2017

let string = "875.43";
let number: Number = new Number(string);
console.log(typeof number);
console.log(number);

and second way:

let number: Number = string as Number;
TypedSource
  • 708
  • 4
  • 12
0

The interviewer is looking for you to write an implementation of parseNumber.

You'd walk the digits and add each digit + currentValue * 10 '.' then add add each digit times a multiplier digit * mult + currentValue; mult *= .1

function parseNumber(s) {
  var value = 0;
  var sign = 1;
  var beforeDecimal = true;
  var mult = .1;
  var ndx = 0;
  var len = s.length;
  
  if (s[0] === '-') {
    sign = -1;
    ndx = 1;
  }
  
  while (ndx < len) {
    var c = s[ndx];
    if (c === '.') {
      if (beforeDecimal) {
        beforeDecimal = false; 
      } else {
        // it's a second '.'. What to do? Error? Exit?
        break;
      }
    } else if (c >= '0' || c <= '9') {
      if (beforeDecimal) {
        value = value * 10 + c.charCodeAt(0) - 48; // 48 = '0'
      } else {
        value = value + c * mult;
        mult *= .1;
      }
    } else {
      // what to do? Error, Exit?
      break;
    }
    ++ndx;
  }
  
  return sign * value;
}

console.log(parseNumber("123"));
console.log(parseNumber("-123"));
console.log(parseNumber("123.456"));
console.log(parseNumber("-123.456"));
console.log(parseNumber("-.42"));
console.log(parseNumber(".42"));

This doesn't handle things like 1E9 and it doesn't handle a preceding +.

You might discuss separating the before and after decimal parts

function parseNumber(s) {
  var value = 0;
  var sign = 1;
  var mult = .1;
  var ndx = 0;
  var len = s.length;
  
  if (s[0] === '-') {
    sign = -1;
    ndx = 1;
  }
  
  // before decimal
  while (ndx < len) {
    var c = s[ndx++];
    if (c === '.') {
      break;
    } else if (c >= '0' || c <= '9') {
      value = value * 10 + c.charCodeAt(0) - 48; // 48 = '0'
    } else {
      // what to do? Error, Exit?
      break;
    }
  }
  
  // after decimal
  if (c === '.') {  // make sure we broke loop above for '.'
    while (ndx < len) {
      var c = s[ndx++];
      if (c >= '0' || c <= '9') {
          value = value + c * mult;
          mult *= .1;
      } else {
        // what to do? Error, Exit?
        break;
      }
    }
  }
  
  return sign * value;
}

console.log(parseNumber("123"));
console.log(parseNumber("-123"));
console.log(parseNumber("123.456"));
console.log(parseNumber("-123.456"));
console.log(parseNumber("-.42"));
console.log(parseNumber(".42"));

You might also consider what to do about errors (return NaN?), and if you were to implement parseInt handling the radix argument.

gman
  • 100,619
  • 31
  • 269
  • 393
0

http://codepen.io/anon/pen/NjPJXg

JS:

console.log(typeof ("875.43"/1));

Output:

number
adamj
  • 4,672
  • 5
  • 49
  • 60
0

Unary Plus:

var str = "875.43";
console.log( str, typeof str )
console.log( +str, typeof +str )
vol7ron
  • 40,809
  • 21
  • 119
  • 172