Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Funcion para darle formato a un número. #javascript #numbers
function number_format(amount, decimals) {
amount += ''; // por si pasan un numero en vez de un string
amount = parseFloat(amount.replace(/[^0-9\.]/g, '')); // elimino cualquier cosa que no sea numero o punto
decimals = decimals || 0; // por si la variable no fue fue pasada
// si no es un numero o es igual a cero retorno el mismo cero
if (isNaN(amount) || amount === 0)
return parseFloat(0).toFixed(decimals);
// si es mayor o menor que cero retorno el valor formateado como numero
amount = '' + amount.toFixed(decimals);
var amount_parts = amount.split('.'),
regexp = /(\d+)(\d{3})/;
while (regexp.test(amount_parts[0]))
amount_parts[0] = amount_parts[0].replace(regexp, '$1' + ',' + '$2');
return amount_parts.join('.');
}
@byWhish

This comment has been minimized.

Copy link

commented May 18, 2016

Muchas gracias por esto me ayudo un monton.

@jPablo23

This comment has been minimized.

Copy link

commented Aug 11, 2016

Excelente! muchas gracias! amigo.

@jmunozrojas-chileautos

This comment has been minimized.

Copy link

commented Apr 6, 2017

Muchas gracias, está genial!!!!

@cuatl

This comment has been minimized.

Copy link

commented Apr 25, 2017

bien, utilicé tu función aquÍ https://tar.mx/apps/honorarios/

@felipedes

This comment has been minimized.

Copy link

commented May 11, 2017

Gracias bro!, me sirvió totalmente :)

@mikote2000

This comment has been minimized.

Copy link

commented Jun 10, 2017

Gracias, me ha sido muy útil

@hrbullon

This comment has been minimized.

Copy link

commented Jul 2, 2017

Gracias, me ha servido mucho, resolví un problema para el cálculo de cotizaciones!

@SOFTDromedicas

This comment has been minimized.

Copy link

commented Jul 6, 2017

Muchas gracias .. Muy util

@npcsayfail

This comment has been minimized.

Copy link

commented Sep 16, 2017

excelente, muy util

@diuardet

This comment has been minimized.

Copy link

commented Nov 15, 2017

Muchas gracias! Perfecto.

@zipus

This comment has been minimized.

Copy link

commented Jan 19, 2018

Perfecto, solo le falta que los números con el símbolo "+" o "-" en el inicio se mantengan.

@ceshuete

This comment has been minimized.

Copy link

commented Jan 26, 2018

Me ayudo bastante tu función bro.

para mantener el signo hago lo siguiente.
como primer linea de la function:
var sign = (amount.toString().substring(0, 1) == "-");
y en el return
return sign ? '-' + amount_parts.join('.') : amount_parts.join('.');

Espero les funcione como a mi.

@canoqb10

This comment has been minimized.

Copy link

commented Feb 12, 2018

Gracias excelente!

@CNavarro117

This comment has been minimized.

Copy link

commented Mar 1, 2018

Muy buen aporte Gracias.

@Th3Khan31

This comment has been minimized.

Copy link

commented Apr 12, 2018

Gracias amigo excelente muy bueno. me salvaste...!

@manicho

This comment has been minimized.

Copy link

commented May 4, 2018

Muchas gracias, era lo que estaba buscando. Gracias por difundir el conocimiento!

@jicamachov

This comment has been minimized.

Copy link

commented Jun 21, 2018

¡Esta genial bro!, saludos desde Arjona-Bolivar - Colombia.

@rigo42

This comment has been minimized.

Copy link

commented Jul 16, 2018

Recomendado !!

@whary11

This comment has been minimized.

Copy link

commented Jul 24, 2018

Gracias, ha funcionado de maravilla para lo que necesito.

@du-hernandez

This comment has been minimized.

Copy link

commented Oct 4, 2018

Gracias, funcionó muy bien

@andresfgiraldo

This comment has been minimized.

Copy link

commented Mar 31, 2019

Excelente! gracias por el aporte.

Para mantener el simbolo:

//amount = parseFloat(amount.replace(/[^0-9.]/g, '')); // elimino cualquier cosa que no sea numero o punto
amount = parseFloat(amount.replace(/[^0-9.-]/g, '')); // andresfgiraldo: En la expresión regular se agrega "-" despues del punto para permitir negativos.

number_format

@dchurapa

This comment has been minimized.

Copy link

commented May 8, 2019

Excelente, muchas gracias me ayudo bastante.

@tcmMexico

This comment has been minimized.

Copy link

commented May 29, 2019

Muchas Gracias!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.