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

byWhish commented May 18, 2016

Muchas gracias por esto me ayudo un monton.

@jPablo23

This comment has been minimized.

Copy link

jPablo23 commented Aug 11, 2016

Excelente! muchas gracias! amigo.

@jmunozrojas-chileautos

This comment has been minimized.

Copy link

jmunozrojas-chileautos commented Apr 6, 2017

Muchas gracias, está genial!!!!

@cuatl

This comment has been minimized.

Copy link

cuatl commented Apr 25, 2017

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

@felipedes

This comment has been minimized.

Copy link

felipedes commented May 11, 2017

Gracias bro!, me sirvió totalmente :)

@mikote2000

This comment has been minimized.

Copy link

mikote2000 commented Jun 10, 2017

Gracias, me ha sido muy útil

@hrbullon

This comment has been minimized.

Copy link

hrbullon 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

SOFTDromedicas commented Jul 6, 2017

Muchas gracias .. Muy util

@npcsayfail

This comment has been minimized.

Copy link

npcsayfail commented Sep 16, 2017

excelente, muy util

@diuardet

This comment has been minimized.

Copy link

diuardet commented Nov 15, 2017

Muchas gracias! Perfecto.

@zipus

This comment has been minimized.

Copy link

zipus 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

ceshuete 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

canoqb10 commented Feb 12, 2018

Gracias excelente!

@CNavarro117

This comment has been minimized.

Copy link

CNavarro117 commented Mar 1, 2018

Muy buen aporte Gracias.

@Th3Khan31

This comment has been minimized.

Copy link

Th3Khan31 commented Apr 12, 2018

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

@manicho

This comment has been minimized.

Copy link

manicho commented May 4, 2018

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

@jicamachov

This comment has been minimized.

Copy link

jicamachov commented Jun 21, 2018

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

@rigo42

This comment has been minimized.

Copy link

rigo42 commented Jul 16, 2018

Recomendado !!

@whary11

This comment has been minimized.

Copy link

whary11 commented Jul 24, 2018

Gracias, ha funcionado de maravilla para lo que necesito.

@du-hernandez

This comment has been minimized.

Copy link

du-hernandez commented Oct 4, 2018

Gracias, funcionó muy bien

@andresfgiraldo

This comment has been minimized.

Copy link

andresfgiraldo 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

dchurapa commented May 8, 2019

Excelente, muchas gracias me ayudo bastante.

@ghost

This comment has been minimized.

Copy link

ghost commented May 29, 2019

Muchas Gracias!!!

@ZeroxBit

This comment has been minimized.

Copy link

ZeroxBit commented Sep 30, 2019

Muchas Gracias!!

@beatlion

This comment has been minimized.

Copy link

beatlion commented Feb 19, 2020

Gracias!!
Buen código.

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.