math.round数字格式

时间:2016-03-05 17:31:32

标签: javascript

我在我的网站上使用了一个制作转换权重的脚本。 Math.round函数正常工作,但我需要将“result”格式化为如下所示:ex。 1.000,000。今天这个数字显示为1000.000。有谁知道怎么办?我尝试了一些number_format但不能的东西。请遵循以下代码:

<script>
        function convert() {

            var amount = document.getElementById("amount").value,
                measurement_from = document.getElementById("measurement_from"),
                measurement_from_name = measurement_from.options[measurement_from.selectedIndex].text,
                measurement_from_type = measurement_from.options[measurement_from.selectedIndex].getAttribute('data-type'),
                measurement_from_value = measurement_from.options[measurement_from.selectedIndex].getAttribute('data-value'),
                measurement_to = document.getElementById("measurement_to"),
                measurement_to_name = measurement_to.options[measurement_to.selectedIndex].text,
                measurement_to_type = measurement_to.options[measurement_to.selectedIndex].getAttribute('data-type'),
                measurement_to_value = measurement_to.options[measurement_to.selectedIndex].getAttribute('data-value'),
                ingredient = document.getElementById("ingredient"),
                ingredient_name = ingredient.options[ingredient.selectedIndex].text,
                ingredient_density = ingredient.options[ingredient.selectedIndex].getAttribute('data-density'),
                total_volume,
                result,
                result_text,
                result_div = document.getElementById("result");

                if (measurement_from_type == 'weight')
                    total_volume = amount * measurement_from_value / ingredient_density;
                else 
                    total_volume = amount * measurement_from_value;

                if (measurement_to_type == 'weight')
                    result = total_volume / measurement_to_value * ingredient_density;
                else
                    result = total_volume / measurement_to_value;

                result = Math.round(result * 1000) / 1000;
                result_text = result + ' ' + measurement_to_name ;
                while( result_div.firstChild ) {
                    result_div.removeChild( result_div.firstChild );
                }
                result_div.appendChild( document.createTextNode(result_text) );


        }
    </script>

谢谢!

2 个答案:

答案 0 :(得分:0)

如果你想要&#39;大陆风格&#39;数字,如1.234.567,89,看看php Locales。

答案 1 :(得分:0)

使用toLocaleString。例如:

public class BaseEntity { }
public class BaseResource { }
public class EntityService<E extends BaseEntity, R extends BaseResource> { }

public class TestEntity extends BaseEntity { }
public class TestResource extends BaseResource { }
public class TestEntityService extends EntityService<TestEntity, TestResource> { }

public class EntityRsqlPredicateBuilder<
  S extends EntityService<E, R>, 
  E extends BaseEntity, 
  R extends BaseResource> 
{
  EntityRsqlPredicateBuilder() {
    // I would like to get the the class type for E (i.e. TestEntity.class) 
    Class<E> entityClass = ???
  }
}

// in code, use EntityRsqlPredicateBuilder
EntityRsqlPredicateBuilder<TestEntityService> = new EntityRsqlPredicateBuilder();

您可以通过更改“de-DE”来选择您喜欢的本地化。 在你的代码上:

var number = 35000000;

console.log(number.toLocaleString('de-DE', { minimumFractionDigits: 2 }));
// outputs "35.000.000,00"