In my application user enters price in text field. So I take the price from textfield like below
NSNumber *price = [NSNumber numberWithDouble:[[self stringAfterRemovingCurrencySymbol:self.priceTextField.text] doubleValue]];
When I print the value like below
DLog(#"Price Value 1: %f",[[self stringAfterRemovingCurrencySymbol:self.buyTextField.text] doubleValue]);
DLog(#"Price Value 2: %#",price);
Here is output (Lets say user enters 150.00)
Price Value 1: 150.000000
Price Value 2: 150
What I want is
Price Value 1: 150.000000
Price Value 2: 150.00
If user enters 150.85 , I am getting 150.85 as it is. But when user enters .00 , I am not getting .00
What should I do so that I will get the .00 ?
Thanks in advance.
You can get the NSNumber's value as a double and use standard printf formatting to specify precision:
DLog(#"Price Value 2: %.2f", [price doubleValue]);
Related
I got a price decimal which sometimes can be either 0.00002001 or 0.00002.
I want to display always 3 zeros from the right if the number is like 0.00002 so I'm looking it to be 0.00002000. If the number is 0.00002001 do not add anything.
I came accross some examples and other examplesin msdn and tried with
price.ToString.Format("{0:F4}", price)
but It doesn't actually change anything in the number.
And in the case number is like 123456789 I want it to display 123.456.789 which I've half solved using ToString("N2") but it's displaying also a .00 decimals which I don't want.
Some special cases here between the fractional and whole numbers, so they need to be handled differently.
Private Function formatWithTrailingZeros(number As Double) As String
If number Mod 1 > 0 Then ' has a fractional component
Return $"{number:0.00000000}"
Else
Dim formattedString = $"{number:N2}"
Return formattedString.Substring(0, formattedString.Length - 3)
End If
End Function
Dim price = 0.00002001
Console.WriteLine(formatWithTrailingZeros(price))
price = 0.00002
Console.WriteLine(formatWithTrailingZeros(price))
price = 123456789
Console.WriteLine(formatWithTrailingZeros(price))
price = 123456789.012345
Console.WriteLine(formatWithTrailingZeros(price))
0.00002001
0.00002000
123,456,789
123456789.01234500
If your second case with 123.456.789 is not based on your current culture, then you may need to replace , with . such as
Return formattedString.Substring(0, formattedString.Length - 3).Replace(",", ".")
Since you are using . both as a decimal separator and a thousands separator, I'm not sure how my example of 123456789.012345000 should look, but since you didn't ask, I'm not going to guess.
I have a product table where I save the price and the amount of profit.
I want to display the amount of time displayed differently
for example :
price : 8.08
profit : 8
Presentable : 8.08 + (8.08 * 8 / 100) = 8.7264
Now I want the answer to be two decimal places and rounded up.
And the answer should be as follows:
Presentable : 8.08 + (8.08 * 8 / 100) = 8.73
The "C" currency format specifier should do the trick for you.
Dim amount As Decimal = 123.456
System.Console.WriteLine(amount.ToString("C", System.Globalization.CultureInfo.CurrentCulture))
' Prints : $123.46
More info here: https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#CFormatString
There is a also a Round method, where you can specify to how many decimal places you would like to round the number. So in your case of rounding to 2 decimal places you could make a call like:
System.Math.Round(amount, 2, System.MidPointRounding.AwayFromZero)
Is it possible to come up with a format which does following
Round off the value.
Show % symbol without multiplying the number with 100
If the value is negative show the number in red color with brackets along with % sign.
Ex: If the value is:
-5.34 should be converted as (5%) in red color
-4.78 should be converted as (5%) in red color
6.33 should be converted as 6 in regular color
Currently I am using 0.00##\%;[Red](0.00##\%) format which shows % symbol wihtout multiplying value with 100 & show negative number in red color with brackets around the value but it does NOT round off the values.
Thank you
Following format is doing the trick for me : ##0\%;[Red](##0\%)
i found this
Is it possible to use format strings to align NSStrings like numbers can be?
I have a similar task but the opposite format.
TOTAL: 2
SUBTOTAL: 6.00
TAX: 0%
CASH: 12.00
my code is this
NSString* item [NSString stringWithFormat:#"%-10s %#", #"TOTAL",#"2"];
Yet outcome is this
TOTAL: 2
SUBTOTAL: 6.00
UPDATE
I got it to work with this code.
NSString* item [NSString stringWithFormat:#"%12s %7s", [total UTF8String],[amount UTF8String]];
UPDATE QUESTION
What if the text is too long?
THIS TEXT IS TOO LONG
I want it to be
THIS TEXT
IS TOO LONG
How to convert a whole number into decimal..For example 70 to .70
because there are some price in my system that needed to be calculated by grams
for example $5 per grams...
I am saving the price in my database as an integer..
Thank's for dropping by
I didn't use vb for a long time, but, I guess, simple float division will work
Dim number As Integer
Dim result As Decimal
number = 70
result = number / 100.0
As you are storing cents and you want to display dollars divide the cents from the database by 100 and show that number in the user interface.
If the user enters an amount of dollars (and cents) multiply by 100 before storing in the database.