how to fix double in flutter even if the last decimal is 0 - dart

Hi so i have a double seems like this
d1 = 12.106
and i need to show in to a string '12.130'
double num1 = double. parse((12.3404). toStringAsFixed(3));
well i expected to return "12.130" but it returned "12.13"
the thing i need a string "12.130" instead "12.13"
double num1 = double. parse((12.3404). toStringAsFixed(4));
so tried this one again but also failed
well then number must be shown 3 decimals even if the last is 0
where or what should i have to fix?

I understand you have the following variable:
double d = 12.130;
and now you want to convert it to a string with the following value: String s = "12.130"
This should work:
String res = d.toStringAsFixed(3) //"12.130"

Related

How to get the last n-characters in a string in Dart?

How do I get the last n-characters in a string?
I've tried using:
var string = 'Dart is fun';
var newString = string.substring(-5);
But that does not seem to be correct
var newString = string.substring(string.length - 5);
Create an extension:
extension E on String {
String lastChars(int n) => substring(length - n);
}
Usage:
var source = 'Hello World';
var output = source.lastChars(5); // 'World'
While #Alexandre Ardhuin is correct, it is important to note that if the string has fewer than n characters, an exception will be raised:
Uncaught Error: RangeError: Value not in range: -5
It would behoove you to check the length before running it that way
String newString(String oldString, int n) {
if (oldString.length >= n) {
return oldString.substring(oldString.length - n)
} else {
// return whatever you want
}
}
While you're at it, you might also consider ensuring that the given string is not null.
oldString ??= '';
If you like one-liners, another options would be:
String newString = oldString.padLeft(n).substring(max(oldString.length - n, 0)).trim()
If you expect it to always return a string with length of n, you could pad it with whatever default value you want (.padLeft(n, '0')), or just leave off the trim().
At least, as of Dart SDK 2.8.1, that is the case. I know they are working on improving null safety and this might change in the future.
var newString = string.substring((string.length - 5).clamp(0, string.length));
note: I am using clamp in order to avoid Value Range Error. By that you are also immune to negative n-characters if that is somehow calculated.
In fact I wonder that dart does not have such clamp implemented within the substring method.
If you want to be null aware, just use:
var newString = string?.substring((string.length - 5).clamp(0, string.length));
I wrote my own solution to get any no of last n digits from a string of unknown length, for example the 5th to the last digit from an n digit string,
String bin='408 408 408 408 408 1888';// this is the your string
// this function is to remove space from the string and then reverse the
string, then convert it to a list
List reversed=bin.replaceAll(" ","").split('').reversed.toList();
//and then get the 0 to 4th digit meaning if you want to get say 6th to last digit, just pass 0,6 here and so on. This second reverse function, return the string to its initial arrangement
var list = reversed.sublist(0,4).reversed.toList();
var concatenate = StringBuffer();
// this function is to convert the list back to string
list.forEach((item){
concatenate.write(item);
});
print(concatenate);// concatenate is the string you need

DXL - Unexpected character output

I'm writing a function to replace substrings (what laguange doesn't have this, grr), and I am getting some strange characters in my ouput. I cannot figure out why.
string replaceSubstring(string input, string targetSubstring, string substitute, bool matchCase)
{
string result = input
Buffer b = create
b = input
int targetStartPos
int targetLength
while (findPlainText(result, targetSubstring, targetStartPos, targetLength, matchCase))
{
string prefixStr = b[0:targetStartPos - 1]
string suffixStr = b[targetStartPos + targetLength:]
b = prefixStr substitute suffixStr
result = tempStringOf(b)
}
delete b
return result
}
When running print replaceSubstring("Jake Lewis", "ake", "ack", false), I get an output of �*��*�is. This would appear to be some sort of encoding issue, but I am unclear on how this is happening, or how to fix it.
Try using stringOf() instead of tempStringOf(). Your processing is fine, but the result becomes invalid after deleting b.

Convert int to string with leading zeros

I have an integer which I want to convert to a string with leading zeros.
So I have 1 and want to turn it into 01. 14 should turn into 14 not 014.
I tried:
let str = (string 1).PadLeft(2, '0') // visual studio suggested this one
let str = (string 1).PadLeft 2 '0'
let str = String.PadLeft 2 '0' (string 1)
But neither work :(
When I search for something like this with F# I get stuff with printfn but I don't want to print to stdout :/
Disclaimer: This is my first F#
You can use sprintf which returns a string rather than printing to stdout. Any of the print functions that start with an s return a string.
Use %0i to pad with zeroes. Add the length of the intended string between 0 and i. For example, to pad to four zeroes, you can use:
sprintf "%04i" 42
// returns "0042"

Could not find an overload for '/' in Swift [duplicate]

I have converted a String to an Int by by using toInt(). I then tried multiplying it by 0.01, but I get an error that says Could not find an overload for '*' that accepts the supplied argument. Here is my code:
var str: Int = 0
var pennyCount = 0.00
str = pennyTextField.text.toInt()!
pennyCount = str * 0.01
From reading other posts it seems that the answer has to do with the type. For example if the type is set as an Integer then it gets a similar error. I have tried changing the type to an Int, but that doesn't seem to solve the problem.
I have also tried setting the type for 'str' and 'pennyCount' as Floats and Doubles and all combinations of Floats, Doubles, and Ints. My guess is the the problem has to do with toInt() function's conversion of a String to an Integer.
Could someone help clarify what the issue may be?
Swift seems to be fairly picky about implied type casting, so in your example you're multiplying str (an Integer) by 0.01 (a Double) so to resolve the error, you'll need to cast it like this:
var str: Int = 0
var pennyCount = 0.00
str = pennyTextField.text.toInt()!
pennyCount = Double(str) * 0.01

Format decimal values on a BlackBerry

I am displaying a double to the user, but it is printed as 1.00000000001
However, I need only two digits after the decimal point.
There's a class called Formatter that can do the trick. Here's a code snippet:
double value = 1.24790000001;
Formatter formatter = new Formatter();
String formatted = formatter.formatNumber(value, 2);
And, here's a link to the JavaDoc: javax.microedition.global.Formatter
Have you looked at String.format e.g.
String x = String.format("%.2f", number);
http://download.oracle.com/javase/6/docs/api/java/lang/String.html

Resources