Split () integer with Javascript

7
Valor = 19.90;

MyArray = valor.split(".");

The code hangs, integer variable, however,

Valor = "19.90";

MyArray = valor.split(".");

alert(MyArray[0]) = 19;
alert(MyArray[1]) = 90;

I would like to know how to use the split int variable, thank you.

According to solutions, from friends already reputed below, I edit my question for the following way.

No, in the case above, I have the function,

function splitNum (34.50 * 2){
var s = n.toFixed(2).split('.');
var n1 = parseInt(s[0], 10); // 19
var n2 = parseInt(s[1], 10); // 10
return n1+"."+n2;
}

will return me    69.0

But I need you to return 69.00, how could that be? Thank you.

Solved, thank you, have a great afternoon.

function splitNum (34.50 * 2){
var s = n.toFixed(2).split('.');
var n1 = parseInt(s[0], 10); // 19
var n2 = parseInt(s[1], 10); // 10
return s[0]"."s[1];
}
    
asked by anonymous 21.03.2014 / 16:41

7 answers

8

split applies only to strings, not to numeric types. If you have a number with 2 decimal places (or are only interested in the first 2 decimal places) you can turn it into a sting using toFixed .

Valor = 19.90;
Valor.toFixed(2); // "19.90"

var valor2 = 1/3;
valor2.toFixed(2); // "0.33"

0.123456789.toFixed(5); // "0.12345"

From here you can apply split . Remember that if you want to convert back to integer *, it is important to specify the base:

var s = Valor.toFixed(2).split('.');
parseInt(s[0], 10); // 19
parseInt(s[1], 10); // 10

// O que acontece se você omitir a base?
var s = 0.01.toFixed(3).split('.'); // ["0", "010"]
parseInt(s[1]); // 8! (começou com zero, então ele interpreta como octal)

* Remembering: if you want! If what you want is a string itself, do not convert back using parseInt , just use the s[0] and s[1] values as you see fit.

    
21.03.2014 / 17:14
4

You do not need to convert to string if the value is numeric.

valor = 19.90;
aValores = [parseInt(valor),Math.ceil((valor  % Math.floor(valor)) * 100)];
    
21.03.2014 / 16:59
2

Just convert to string before using split .

There are several ways to convert a number to string, for example:

var n = 10.01;
var s = n.toString();
var a = s.split('.');

Incidentally, you use the term int and integer to refer to a number that is not integer. Such terms are used only for integers (e.g. 1, 2, 3, 4, ... 1000) and not for rational numbers (e.g. 19.90).

EDIT This part of the answer is wrong, read the comments to understand. You should still want to turn the numbers, after the split, back into numeric type:

var n1 = parseInt(a[0]);
var n2 = a.length > 1 ? parseInt(a[1]) : 0;

The solution in the end, it would look like this

function SplitNum(n)
{
    var s = n.toString();
    var a = s.split('.');
    var n1 = parseInt(a[0]);
    var n2 = a.length > 1 ? parseInt(a[1]) : 0;
    return [n1, n2];
}

The correct way to turn numbers back, would be to keep what comes after the decimal separator, then decimal separator:

var n1 = parseInt(a[0]);
var n2 = a.length > 1 ? parseFloat('0.'+a[1]) : 0;
    
21.03.2014 / 16:52
1

Solution

You first need to turn int / double into a string for example:

Valor = 19.90;

MyArray = String(Valor).split(".");

alert(MyArray[0]) // 19

alert(MyArray[1]) // 9 que no decimal é a mesma coisa que 90
    
21.03.2014 / 16:47
1

As explained in MDN, split () serves to separate strings, and What you are trying to do is separate an integer. What you can do is to convert integer to string:

var n =  123456789;
var digits = (""+n).split("");

Note that this will return you an array of string, not an integer.

    
21.03.2014 / 16:46
1

You can try something like this:

var Valor = 19.9;

var Dados = $.map( Valor.toString().split("."), function(v){
    return parseInt(v); 
});

jQuery.map () - link

    
21.03.2014 / 17:26
1

Answering your question "Split integer with javascript" :

It's not possible. Because Javascript does not work with Integer, it works only with Number .

But I see that you want a method that accomplishes this for you without you needing to use lines of code, since it is the goal of .split() , so I will make you a function assigning it to Number.prototype so that you can use same as .split() :

Number.prototype.split = function(){
  var a = this.toString().split(".");
  return [parseInt(a[0]),(a[1].length > 1) ? parseInt(a[1]) : (parseInt(a[1])*10)];
} 
var Valor = 19.90;
Valor.split(); //retorna um array [ 19, 90 ]

var Valor = 19.95;
Valor.split(); //retorna um array [ 19, 95 ]

var Valor = 19.957;
Valor.split(); //retorna um array [ 19, 957 ]

Note that it ignores the zero in the end because it is a Number and speaking in numbers 19.90 is the same as 19.9 , so I did a check if the size of the number is greater than 1 it multiplies by 10 so always having your "zero" you want.

    
21.03.2014 / 17:28