Jump to content


Photo

Formatar String, Casas Decimais


  • Faça o login para participar
4 replies to this topic

#1 kbalera

kbalera

    Normal

  • Usuários
  • 108 posts
  • Sexo:Não informado
  • Localidade:<? Ipatinga?>
  • Interesses:<? print "Php - WebDesigner"; ?>

Posted 30/09/2004, 15:34

Ai galera do precisando de fazer o seguinte
eu tenho uma variavel

tipo

var a = 1;

so q eu kero fazer ela ter o valor de

0001 -> tipo com quatro casas decimais

tipo formatar uma variavel pra ter4 casas decimais
em php eu consigo fazer mas to precisando fazer em javascript
sera que alguem pode me ajudar?


JUNINHO
<?php
print "nick = Kbalera";
print "email = junior@rockiando.com.br";
print "cidade = IPATINGA/MG";
print "A arte de criar, é bem maior de que falar que criou";

?>

#2 feubr

feubr

    Normal

  • Usuários
  • 80 posts
  • Sexo:Não informado

Posted 30/09/2004, 16:41

Brother, achei em : http://www.js-x.com/...ript/?viewh=965 a sua solução...

<html>
<head>
<!--
This file retrieved from the JS-Examples archives
http://www.js-x.com
1000s of free ready to use scripts, tutorials, forums.
Author: Buddhike de Silva - 0
-->

<script>
/*function : format_number()
version: 1.0.0
This function formats a numeric value passed in to it with specified number of
decimal values. numeric value will not be rounded.
pnumber : numeric value to be formatted.
decimals : number of decimal points desired.

Author: Buddhike de Silva
Date: 21-Nov-2002 11:16 AM*/

/*
revision: 1.1.0
Author: M. Cassim Farook
Date: 21-Nov-2002 10:16 PM
Notes: No offense buddhike...but i had to rewrite the code
works for ADT (any dam thing)
usage: x = format_number(123.999, 2)
*/

/*
revision: 1.2.0
Authors: Buddhike de Silva
Date: 22-Nov-2002 12:07 PM
Notes: Optimized for best performence.
usage: x = format_number(123.999, 2)
*/

/*
* Revision: 1.3
* Author: Mike Robb (JS-X.com)
* Date: May 26, 2003
* Notes: Changed to deal with negative numbers.
* Fixed length of final answer.
* Work-around for javascript internal math problem with rounding negative numbers.
*/

/*
* Revision 1.4
* Author: LeAnn Roberts
* Date: September, 2003
* Note: Modified the if logic: Math.pow()
*/

/*
* Revision 1.5
* Author: Robert Heggdal
* Date: February, 2004
* Note: Modified check for negative number by replacing parseInt with parseFloat so that negative numbers between zero and minus one are recognized as such.
*/

/*
* Revision 1.6
* Author: Naveen
* Date: February, 2004
* Note: Rewrote format_number to correct a logic problem.
*/

/*
* Revision 1.7
* Author: JS-X.com
* Date: February, 2004
* Note: Added wrapper around format_number as negative values were dropped from
* the logic.
*/

function format_number(p,d)
{
var r;
if(p<0){p=-p;r=format_number2(p,d);r="-"+r;}
else {r=format_number2(p,d);}
return r;
}
function format_number2(pnumber,decimals)
{
var strNumber = new String(pnumber);
var arrParts = strNumber.split('.');
var intWholePart = parseInt(arrParts[0],10);
var strResult = '';
if (isNaN(intWholePart))
intWholePart = '0';
if(arrParts.length > 1)
{
var decDecimalPart = new String(arrParts[1]);
var i = 0;
var intZeroCount = 0;
while ( i < String(arrParts[1]).length )
{
if( parseInt(String(arrParts[1]).charAt(i),10) == 0 )
{
intZeroCount += 1;
i += 1;
}
else
break;
}
decDecimalPart = parseInt(decDecimalPart,10)/Math.pow(10,parseInt(decDecimalPart.length-decimals-1));
Math.round(decDecimalPart);
decDecimalPart = parseInt(decDecimalPart)/10;
decDecimalPart = Math.round(decDecimalPart);

//If the number was rounded up from 9 to 10, and it was for 1 'decimal'
//then we need to add 1 to the 'intWholePart' and set the decDecimalPart to 0.

if(decDecimalPart==Math.pow(10, parseInt(decimals)))
{
intWholePart+=1;
decDecimalPart="0";
}
var stringOfZeros = new String('');
i=0;
if( decDecimalPart > 0 )
{
while( i < intZeroCount)
{
stringOfZeros += '0';
i += 1;
}
}
decDecimalPart = String(intWholePart) + "." + stringOfZeros + String(decDecimalPart);
var dot = decDecimalPart.indexOf('.');
if(dot == -1)
{
decDecimalPart += '.';
dot = decDecimalPart.indexOf('.');
}
var l=parseInt(dot)+parseInt(decimals);
while(decDecimalPart.length <= l)
{
decDecimalPart += '0';
}
strResult = decDecimalPart;
}
else
{
var dot;
var decDecimalPart = new String(intWholePart);

decDecimalPart += '.';
dot = decDecimalPart.indexOf('.');
var l=parseInt(dot)+parseInt(decimals);
while(decDecimalPart.length <= l)
{
decDecimalPart += '0';
}
strResult = decDecimalPart;
}
return strResult;
}

</script>

</head>
<body>

<form name=f0 onsubmit="document.f0.r1.value=format_number(document.f0.t1.value,document.f0.t2.value);return false;">
Number<input type=text name=t1 value="0.9666666666666667"><BR>
Percision<input type=text name=t2 value="1"><BR>
<input type=submit value="Format Number"><BR>
Result <input type=text name=r1 value="?">
</form>
<BR>
<form name=f1 onsubmit="document.f1.r1.value=format_number(document.f1.t1.value,document.f1.t2.value);return false;">
Number<input type=text name=t1 value="123.546789"><BR>
Percision<input type=text name=t2 value="3"><BR>
<input type=submit value="Format Number"><BR>
Result <input type=text name=r1 value="?">
</form>
<BR>
This will divide num1 / num2 then compute
<form name=f2 onsubmit="document.f2.r1.value=format_number(document.f2.t1.value/document.f2.t2.value,document.f2.t3.value);return false;">
Number1<input type=text name=t1 value="22"><BR>
Number2<input type=text name=t2 value="7"><BR>
Percision<input type=text name=t3 size=30 value="15"><BR>
<input type=submit value="Format Number"><BR>
Result <input type=text name=r1 value="?">
</form>

<BR><center><a href='http://www.js-x.com'>JS-X.com</a></center>
</body>
</html>

#3 HaroNism

HaroNism

    Super Veterano

  • Usuários
  • 15385 posts
  • Sexo:Masculino
  • Localidade:San Miguel de Tucuman

Posted 19/09/2017, 02:48

Drugs Onn Line cialis Where To Buy Azithromycin In Canada
Usa Rx Pharmacy Cialis Effexor Buy Online
Viagra Kaufen Dresden cialis Body Mass

#4 HaroNism

HaroNism

    Super Veterano

  • Usuários
  • 15385 posts
  • Sexo:Masculino
  • Localidade:San Miguel de Tucuman

Posted 05/10/2017, 04:59

On Sale Worldwide Amoxicilina Cherche Viagra Suisse Buy Brand Name Accutane buy viagra online Viagra Duree Efficacite
Cialis Effet Duree viagra online pharmacy Cialis Lyon

#5 HaroNism

HaroNism

    Super Veterano

  • Usuários
  • 15385 posts
  • Sexo:Masculino
  • Localidade:San Miguel de Tucuman

Posted 30/10/2017, 00:40

Levitra Hoher Blutdruck generic viagra Get My Canadian Drugs Comprare Il Cialis In Farmacia Free Shipping Isotretinoin Acne Buy Low Price
Generique Amoxicillin Pharmacie En Ligne En Suisse Generique Achat Buy Viagra Online In Francia Viagra Zum Bestellen viagra online prescription Usa Viagra Delivery Cialis Muskelaufbau Cialis Y Hipertension
Comprar Cialis Original Generico Kamagra 100mg Di Gel Kamagra Etanol how cau you order levitra on line Kamagra Oral Jelly Legal In Deutschland Levitra 20mg Filmtabletten Preisvergleich




1 user(s) are reading this topic

0 membro(s), 1 visitante(s) e 0 membros anônimo(s)

IPB Skin By Virteq