How to Format a Number to 2 Decimal Places, But Only If There Are Already Decimals

How do I format a number to 2 decimal places, but only if there are already decimals?

Working example: http://jsfiddle.net/peeter/JxPZH/

$(document).ready(function() {    $('#itemQuantitySelect_3').change(function() {                var itemPrice = 1.50;        var itemQuantity = $(this).val();        var quantityPrice = (itemPrice * itemQuantity);        if(Math.round(quantityPrice) !== quantityPrice) {            quantityPrice = quantityPrice.toFixed(2);        }                $(this).next("span").html("$" + quantityPrice);
});});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><form action="/" method="post">    <select id='itemQuantitySelect_3' name="itemQuantity_3">        <option value='1'>1 Item</option>        <option value='2'>2 Items</option>        <option value='3'>3 Items</option>    </select>    <span>$1.50</span></form>

Format number to 2 decimal places only if it has decimal places

I want it to work with one single format function

Well, that's not possible. D3 format functions cannot adapt to the number you pass to them like that.

However, you can have two format functions, and test to see if the number you're passing has or has not decimal places. This is one possible way to test:

number % 1

Which returns 0 for integers.

Then, in a ternary operator, you can choose which d3.format function you'll use:

!(number % 1) ? someFormat(number) : otherFormat(number)

Here is a demo:

var formatInteger = d3.format(",");var formatDecimal = d3.format(",.2f");
var number1 = 10101;var number2 = 12334.2;
function format(number){return !(number % 1) ? formatInteger(number) : formatDecimal(number)}
console.log(format(number1))console.log(format(number2))
<script src="https://d3js.org/d3.v4.min.js"></script>

Format number to 2 decimal places

You want to use the TRUNCATE command.

https://dev.mysql.com/doc/refman/8.0/en/mathematical-functions.html#function_truncate

Using String Format to show decimal up to 2 places or simple integer

An inelegant way would be:

var my = DoFormat(123.0);

With DoFormat being something like:

public static string DoFormat( double myNumber )
{
var s = string.Format("{0:0.00}", myNumber);

if ( s.EndsWith("00") )
{
return ((int)myNumber).ToString();
}
else
{
return s;
}
}

Not elegant but working for me in similar situations in some projects.

How to display two digits after decimal point in SQL Server

select cast(your_float_column as decimal(10,2))
from your_table

decimal(10,2) means you can have a decimal number with a maximal total precision of 10 digits. 2 of them after the decimal point and 8 before.

The biggest possible number would be 99999999.99

How do I display a decimal value to 2 decimal places?

decimalVar.ToString("#.##"); // returns ".5" when decimalVar == 0.5m

or

decimalVar.ToString("0.##"); // returns "0.5"  when decimalVar == 0.5m

or

decimalVar.ToString("0.00"); // returns "0.50"  when decimalVar == 0.5m

VBA How to format to 2 decimal places only if decimals required

I would use a sub like this - before applying the numberformat it checks for errors or text - and quits. By that you are sure that only numbers are handled.

Sub reformatNumber(c As Range)

If IsError(c.value) Then Exit Sub
If Not IsNumeric(c.Value2) Then Exit Sub

If Int(c.Value2) <> c.Value2 Then
c.NumberFormat = "0.00"
Else
c.NumberFormat = "0"
End If

End Sub

You can test it like this:

Sub test_reformat()
Dim c As Range
For Each c In Selection
reformatNumber c
Next
End Sub


Related Topics



Leave a reply



Submit